1
votes

I am trying to create a trigger in APEX, when an custom textfield of an custom sObject gets updated with products (means, when new products get insert or existing one get deleted).

How can I compare in APEX the Trigger.Old values with the Trigger? New values of this field in order to start the Trigger.

It would look something like this:

Trigger NameOfTrigger on CustomSObject__c (after update){

/*there is already an existing list of products that get insert into the custom textfield (probably as Strings)
*/

List <String> textList = new List <String> (); 

/*PseudoCode: if the textfield got updated/has changed, copy from every entry of this textfield (entry = product name as a string) and copy fieldX into another sObject
*/

if(CustomSObject.field(OldValues) != CustomSObject.field(NewValues)){
for (String product : textList){
   //Trigger e.g. copy the values of a certain field of p and paste them in another sObject
}

Could somebody help me with the syntax?

1

1 Answers

0
votes

You can utilize inbuilt Trigger.new and Trigger.old to get the latest and old values of any record. These lists can be used to achieve what you're looking for.

Sample example would be:

Trigger NameOfTrigger on CustomSObject__c (after update){

    for(CustomSObject__c customObject : Trigger.new) {

        // get old record
        CustomSObject__c oldCustomObject = Trigger.oldMap.get(customObject.Id);

        // compare old and new values of a particular field
        if(customObject.fieldName != oldCustomObject.fieldName){
            //Trigger e.g. copy the values of a certain field of p and paste them in another sObject
        }

    }

}

See documentation of Trigger.new & Trigger.old