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
Phani PYDIMARRY 11Phani PYDIMARRY 11 

Compare trigger Not Working

Hello,

I am trying to compare the New value in a field which is a string and if it contains 'NO CHANGE' in it, then I would want to replace it by the Old Value. Below is my code. It seems to be not considering Contains expression as it it taking NO CHANGE but not a scentense where NO CHANGE is present. like THIS. HAS NO CHANGE. Below is my code in Before Update

Old Record - This is New 
New Record - this is new - NO CHANGE
Trigger fires and then changes to Old Record - This is New 

 
for(sObject newRecobj: Trigger.new){

myObject__C newRec = (myObject__C) newRecObj;

SObject oldRecObj = Trigger.oldMap.get(newRec.Id);

myObject__C oldRec = (myObject__C) oldRecObj;

if('NO CHANGE'.contains(newRec._NOTE__c) 
    && !string.isBlank(oldRec.NOTE__c) 
&& !'NO CHANGE'.contains(oldRec.NOTE__c)){

                newRec.NOTE__c = oldRec.NOTE__c;
            }

}

What i am I doing wrong here? 
Best Answer chosen by Phani PYDIMARRY 11
Pawel BorysPawel Borys
Hi,
If I understand correctly what you mean then you should do the check the other way around. Now you are checking if the string 'NO CHANGE' contains the value of your field. So what you want to do is newRec._NOTE__c.contains('NO CHANGE') 

All Answers

Pawel BorysPawel Borys
Hi,
If I understand correctly what you mean then you should do the check the other way around. Now you are checking if the string 'NO CHANGE' contains the value of your field. So what you want to do is newRec._NOTE__c.contains('NO CHANGE') 
This was selected as the best answer
Phani PYDIMARRY 11Phani PYDIMARRY 11
BANG ON . I was always checking for the other way. Thanks. This is a great learning