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
HoysalaHoysala 

If any account Phone has changed then update same phone number to all related contacts

Best Answer chosen by Hoysala
Alan DePewAlan DePew
trigger AccountTrigger on Account (after insert, after update) {

Set<ID> ids = Trigger.newMap.keySet();

List<Account> Accts = [SELECT Id, Phone, (select ID, AccountID from Contacts) 
    FROM Account 
    WHERE Id in :ids];

List<Contact> ContactstoUpdate = new List<Contact>();

for(Account acc : Accts)
 {
  String oldvalue = Trigger.oldMap.get(acc.Id).Phone;
  String newValue = acc.Phone;

	if(oldvalue != newValue )
  	{
    	for(Contact C: acc.Contacts) 
     	{                      
      	C.Phone = Acc.Phone; 
      	ContactstoUpdate.add(C);
     	}
	}
 }

if( !ContactstoUpdate.isEmpty() )
{ 
  update ContactstoUpdate;
 }
    
}