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
Luke Higgins 22Luke Higgins 22 

Replace string in Rich Text Area upon save

I am trying to remove and replace a few strings that are rendering incorrectly for a Rich Text field through an Apex Trigger. Any idea what is incorrect with the following code?
 
trigger removeCharacters on ts2__Job__c (before insert) {
    String str = '\u00A0';
    String str2 = 'Â';
    String replacement = ' ';
    for(ts2__Job__c des : trigger.new){
        if(des.ts2__Job_Advertisement__c.contains(str)){
            des.ts2__Job_Advertisement__c.replace(str, replacement);
        }
        if(des.ts2__Job_Advertisement__c.contains(str2)){
            des.ts2__Job_Advertisement__c.replace(str2, replacement);
        }
    }

}

 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Luke,

When working with Rich text area  fields it is not possible to use replace and search Methods.There is an idea posted regardinging this,you can upvote this idea.

https://success.salesforce.com/ideaView?id=0873A000000E6M3QAK


Hope this helps you
If this helps you kindly mark it as solved so that it may help others in future.

Thanks and Regards
Maharajan CMaharajan C
Hi Luke,

Please try the below changes:
you have to assign the value in field with the replaced value.

trigger removeCharacters on ts2__Job__c (before insert) {
    String str = '\u00A0';
    String str2 = 'Â';
    String replacement = ' ';
    for(ts2__Job__c des : trigger.new){
        if(des.ts2__Job_Advertisement__c.contains(str)){
            des.ts2__Job_Advertisement__c = des.ts2__Job_Advertisement__c.replace(str, replacement);
        }
        if(des.ts2__Job_Advertisement__c.contains(str2)){
            des.ts2__Job_Advertisement__c = des.ts2__Job_Advertisement__c.replace(str2, replacement);
        }
    }

}

Thanks,
Maharajan.C
Caroline Poole 9Caroline Poole 9

Hello!

Do you mind sharing the test class that you would use alongside this apex trigger?

Thanks,

Caroline