So now we have understood that trigger.new returns new list of records and trigger.old returns old list of records. Let us try to understand
the same again by taking an example.
SCENARIO: We are going
to update the Account record phone number from a particular value to a new
value and we will using trigger.new
and trigger.old to see the old and
the new value before and after the update is done to a record.
APEX TRIGGER:
trigger AccountMainTrigger on Account (before update) {
createContactClass obj=new
createContactClass();
if(trigger.isbefore &&
trigger.isupdate)
{
obj.method1(trigger.new,trigger.old);
}
}
APEX CLASS:
public class createContactClass {
public void method1(List<Account>
newList,List<Account> oldList){
for(Account obj:newList){
system.debug('New Value of
phone'+obj.phone);
}
for(Account obj1:oldList){
system.debug('Old Value of
phone'+obj1.phone);
}
}
}
Account record in the initial stage,
Now let us update the phone number on the Account record.
Now go to “Developer console” as shown in the below image to
check the debug values,
Any code is added in open execute anonymous window. for checking debug logs
ReplyDeleteNo code is added in execute anonymous window, System.debug in code will be displayed under log.
Delete