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
rakesh kondarakesh konda 

Trigger for updateing the contacts phone number automatically

Hello , 

Please help in wrting the trigger if the phone number in the account tab is changed by the user it must automatically update in the contact tab phone number also

Thanks & Regards,
Rakesh Konda
Best Answer chosen by rakesh konda
Apoorv Saxena 4Apoorv Saxena 4
Hi Rakesh,

Please try the following code :
 
trigger updateContactsPhone on Account (before update) {
   List<Account> accList;
   List<Contact> conList = new List<Contact>();
   Set<Id> accIdSet = new Set<Id>();
   
    for (Account acc : Trigger.New){
		accIdSet.add(acc.id);
    }
	
    if(accIdSet<>null && accIdSet.size()>0){
        accList = [Select id,phone,(Select id,Phone from Contacts) from Account where ID in:accIdSet];
    }
    
    for(Account acc:accList){
        for(Contact con:acc.contacts){
			con.phone = trigger.newMap.get(acc.id).phone;
			conList.add(con);
        }
    }
        
    if(conList<>null && conList.size()>0){     
        update conList;
    }
}

Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Rakesh,

Please try the following code :
 
trigger updateContactsPhone on Account (before update) {
   List<Account> accList;
   List<Contact> conList = new List<Contact>();
   Set<Id> accIdSet = new Set<Id>();
   
    for (Account acc : Trigger.New){
		accIdSet.add(acc.id);
    }
	
    if(accIdSet<>null && accIdSet.size()>0){
        accList = [Select id,phone,(Select id,Phone from Contacts) from Account where ID in:accIdSet];
    }
    
    for(Account acc:accList){
        for(Contact con:acc.contacts){
			con.phone = trigger.newMap.get(acc.id).phone;
			conList.add(con);
        }
    }
        
    if(conList<>null && conList.size()>0){     
        update conList;
    }
}

Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv
This was selected as the best answer
Gordon EngelGordon Engel
I think you could also do this with a process flow (no code required).  If I recall correctly, the trailhead module teaches somethink like this as an exercise.
rakesh kondarakesh konda
Hello Apoorv,

I am confused with the program there is no map in the program but you have used map.get(accid) can you please explain me the program

Thanks,
Rakesh
Apoorv Saxena 4Apoorv Saxena 4
Hi Rakesh,

Using trigger.newMap in a Before Update trigger will let you access the new field values, i.e here trigger.newMap.get(acc.id).phone will give you new value of Phone field for that account, though it is not yet updated.

For more detail on trigger context variable refer to this link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

Hope this helps!

Please mark this as Solved if your question was answered, so that others can view it as a proper solution.

Thanks,
​Apoorv
rakesh kondarakesh konda
Thanks Apoorv and Gordon Engel