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
Pradeep Musthi 9Pradeep Musthi 9 

Updating contact phone whenever account phone is updated

trigger UpdateAccount2 on Account (before update) {

List<Contact> lst=new List<contact>();
List<Account> acc=[select id,phone,(select id,phone from contacts)
                    from account where id in: trigger.newmap.keyset()];
for(Account a:acc)
{

for(Contact c:a.contacts)
{

c.phone=a.phone;
system.debug(c.phone);
lst.add(c);
}
}
update lst;                    
}

This is my Code and Its not working,can anyone what's wrong in my code.
Steven NsubugaSteven Nsubuga
I can't figure out why your code is not working consistently. Try this version:
trigger UpdateAccount2 on Account (before update) {

    List<Contact> contacts=[select id, Accountid, phone  from contact where Accountid in: trigger.newmap.keyset()];
    
        for(Contact c : contacts)
        {
            c.phone=trigger.newmap.get(c.Accountid).phone;
            system.debug(c.phone);
        }   
    update contacts;                    
}

 
Niraj Kr SinghNiraj Kr Singh
Hi Pradeep,
 You are not able to update because of the SOQL doing on Account inside the Account trigger only.  In that case your code has not yet committed in DB, so that you will get old account values only.

Try this code it will work for you.
Trigger:
trigger UpdateAccountContactPh on Account (before update) {

    if(Trigger.isBefore && Trigger.isAfter) {
		UpdateAccountContactPhHandler.UpdateAccountContactPhHandler(Trigger.oldMap, Trigger.newMap);
	}                 
}
Handler Class:
public class UpdateAccountContactPhHandler {
    
    public void UpdateAccountContactPh(Map<Id, Account> oldAccountMap, Map<Id, Account> newAccountMap) {
        List<Contact> contactList = [SELECT AccountId, Phone  FROM Contact WHERE AccountId In: newAccountMap.keySet()];
    
        for(Contact objCon : contactList) {
            if(oldAccountMap.get(objCon.AccountId).Phone != newAccountMap.get(objCon.AccountId).Phone) {
                objCon.phone = newAccountMap.get(objCon.AccountId).Phone;
            }
        }
        try{
            if(!contactList.isEmpty())
                update contactList;
        }catch(DMLException dmlEx) {
            system.debug('----dmlException----' + dmlEx);
        }
    }
}

And Mark your ans if it works
Thanks
Niraj
Ajay K DubediAjay K Dubedi
Hi Pradeep,

Try below code. It's working fine.
----------------Helper Class--------------------

public class Update_Contact_Phone {
    public static void contactPhone(map<Id,Account> newAccMap){
        set <id> IdCollect = newAccMap.keySet();
        List<Contact> toUpdateconList = new List<Contact>();
        if(IdCollect.size() > 0){
            List <Contact> conList = [SELECT Phone,AccountId From Contact WHERE AccountId IN :IdCollect];
            if(conList.size() > 0){                
                for(Contact c : conList){
                    
                        c.Phone = newAccMap.get(c.AccountId).Phone;
                        toUpdateconList.add(c);
                    
                }
            }
            if(toUpdateconList.size() > 0){
                Update toUpdateconList;
            }
        }
    }
}

------------Trigger Class---------------------
trigger UpdateContacttPhoneNumberTrigger on Account (before update) {
    Update_Contact_Phone.contactPhone(Trigger.newMap);
    }

Please mark it as best Answer if you find it helpful.
Please let me know if you have any query.

Thank You
Ajay Dubedi