+ Start a Discussion
Sonam PatilSonam Patil 

Contact update on Account

Hi,
Can anyone help me with this scenario?
What Exactly,  if( a.phone!= trigger.oldmap.get(a.id).phone) line does in the below scenario. What does it mean?


trigger contactUpdate_acc on Account (after update) {
    list<contact> ctc =new list<contact>();
    list<account> acc = new list<Account>();
    for(Account a:trigger.new){
        if( a.phone!= trigger.oldmap.get(a.id).phone){
            acc.add(a);
        }
    }
}
Best Answer chosen by Sonam Patil
Santosh Kumar 275Santosh Kumar 275
Trigger.OldMap contains a map of IDs to the old versions of the sObject records.
and Trigger.new returns a list of the new versions of the sObject records.
So in this line, it is comparing the new value with old value.
 if( a.phone!= trigger.oldmap.get(a.id).phone)

a.phone will contain the new value from trigger.new and
trigger.oldmap.get(a.id).phone will fetch the old value of phone from the map.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Sonam,

This line performs comparison between the previous and new value of phone field on Account object record. Trigger.oldMap contains the older versions of sobjects.
Santosh Kumar 275Santosh Kumar 275
Trigger.OldMap contains a map of IDs to the old versions of the sObject records.
and Trigger.new returns a list of the new versions of the sObject records.
So in this line, it is comparing the new value with old value.
 if( a.phone!= trigger.oldmap.get(a.id).phone)

a.phone will contain the new value from trigger.new and
trigger.oldmap.get(a.id).phone will fetch the old value of phone from the map.
This was selected as the best answer