function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cruttley2cruttley2 

Trigger Help - Product object - I simply want to copy Rich Text field A to Rich Text field B

This has to be the simplest trigger request ever.

 

I have a rich text field on Product object. Lets call it RTA.

I have another rich text field on Product object. Lets call it RTB.

I simply want a trigger that copies RTA to RTB whenever the Product is created or updated. I have never written Apex code before.

 

(Why do I need this? Because this cannot be done through formulas or workflows, because it is a Rich Text field, which Salesforce has limitations on.)

 

I have been looking for similar code, but due to my limited Apex programming skills, I cant figure out how to code this.

 

Could anyone help provide code for this simpe trigger?

 

All help GREATLY appreciated. 

Best Answer chosen by Admin (Salesforce Developers) 
@anilbathula@@anilbathula@
Hi cruttley,

Try this trigger :-

trigger updtrichtextfld on ur_object_name (before insert,before update) {

for(ur_object_name u:trigger.new){
if(u.rta__c!=null){
u.rtb__c=u.rta__c;
}
}
}

This trigger fires when ever rta__c not null.
put your own condition when ever you want this trigger to fire.

All Answers

@anilbathula@@anilbathula@
Hi cruttley,

Try this trigger :-

trigger updtrichtextfld on ur_object_name (before insert,before update) {

for(ur_object_name u:trigger.new){
if(u.rta__c!=null){
u.rtb__c=u.rta__c;
}
}
}

This trigger fires when ever rta__c not null.
put your own condition when ever you want this trigger to fire.
This was selected as the best answer
cruttley2cruttley2
Thanks Anil. I will try that out on Monday.
cruttley2cruttley2

Here is the trigger that I have built, based on your input:

trigger Copy_Product_Terms_To_Product_Description_External on Product2 (before insert,before update) {

for (Product2 op: Trigger.new) {

        IF(op.BMXQ__Product_Terms__c!=null)
            {op.Product_Description_External__c = op.XXXQ__Product_Terms__c;}
    }
}

 

The trigger actually workds great, so thanks for your help. But I cant deploy it, because my test class keeps failing on the second system assert, and I dont know why: Here is my test class:

 

public class TestCopyProductTermsToProductDescExt{  
    static testmethod void Copy_Product_Terms_To_Product_Description_External (){    
        Product2 newproduct = new Product2(Name = 'Chris Test Product', Family = 'Election Management System',Product_Type__c='Other',Quote_Sort_Group__c='70  Other',IsActive = True,XXXQ__Product_Terms__c = 'THIS IS A RICH TEXT DESCRIPTION');
        
        insert newProduct;
        
        system.assertNotEquals(newProduct.Id, null); 

        System.assertEquals('THIS IS A RICH TEXT DESCRIPTION',newProduct.Product_Description_External__c,'THE FIELDS DONT MATCH 2!');
        
        system.debug(newProduct);
    }   
}

 

And the test class failure is:

System.AssertException: Assertion Failed: THE FIELDS DONT MATCH 2!: Expected: THIS IS A RICH TEXT DESCRIPTION, Actual: null

@anilbathula@@anilbathula@
Hi Cruttley2

Insert some value into this Product_Description_External__c field in the test class.

I just modified the test class use this code:-

public class TestCopyProductTermsToProductDescExt{
static testmethod void Copy_Product_Terms_To_Product_Description_External (){

Product2 newproduct = new Product2(Name = 'Chris Test Product',
Family = 'Election Management System',Product_Type__c='Other',
Quote_Sort_Group__c='70  Other',
IsActive = True,
XXXQ__Product_Terms__c = 'THIS IS A RICH TEXT DESCRIPTION',
Product_Description_External__c='THIS IS A RICH TEXT DESCRIPTION');

insert newProduct;

system.assertNotEquals(newProduct.Id, null);

System.assertEquals('THIS IS A RICH TEXT DESCRIPTION',newProduct.Product_Description_External__c,'THE FIELDS DONT MATCH 2!');

system.debug(newProduct);
}
}

If still its is not working just remove system assert function .i feel you wont require these functions in insert operation.



cruttley2cruttley2

Thanks again Anil.

I made the changes, but the AssertEquals still fails. So like you said, I am going to comment out the AssertEquals, since the trigger is working.

But I wish I knew why it was failing!

 

Thanks for your help.

 

Chris