[SalesForce] Check if long text area field contains given string

I have a custom field called Account_from__c which is a text field. Now, I have a Long text field called Warning_Message__c which contains warning messages. The warning message will be like "Account from value cannot be null". This warning message field will contains multiple messages.

Now, if Account_from__c is not null, this particular warning message should be removed.

below is the code snipper:

String s = 'The Account From cannot be blank.';
if(acc.Account_from__c != Null){
                if(acc.Warning_Message__c.contains(s)){
                    acc.Warning_Message__c.replace(s,'');
                }
            }

This logic isnt working as expected. Even when I update the record with Account from value, the message is still in place.

Can anyone please suggest any update to the above code so that i can get this done.

Thanks!

Best Answer

You forgot to actually update the warning message. Replace does not change the current string but generates a new one.

String s = 'The Account From cannot be blank.';
if(acc.Account_from__c != Null){
    if(acc.Warning_Message__c.contains(s)){
        acc.Warning_Message__c = acc.Warning_Message__c.replace(s,'');
    }
}
Related Topic