+ Start a Discussion
Priyesh Misquith 12Priyesh Misquith 12 

trigger to update account phone from the recent created or updated contact phone

Hi There,
can any one help me to write a trigger to update account phone number from the  recent created  contact or recent updated contact phone. 
the scenario should hold if there is multiple contact on account, updated uith the latest  created  contact phone or recent updated contact phone.
Best Answer chosen by Priyesh Misquith 12
Khan AnasKhan Anas (Salesforce Developers) 
Hi Priyesh,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger UpdateAccBasedOnCon on Contact (after insert, after update) {
    
    List<Id> accIds = new List<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Contact c : trigger.new){
        accIds.add(c.accountId);
    }
    
    for(Account a : [SELECT Id, Phone, Rating FROM Account WHERE Id IN :accIds]){
        for(Contact con : trigger.new){
            a.Phone=con.Phone;
            accounts.add(a);
        }
    }
    if(accounts.size()>0){
        UPDATE accounts;        
    } 
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Priyesh,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger UpdateAccBasedOnCon on Contact (after insert, after update) {
    
    List<Id> accIds = new List<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Contact c : trigger.new){
        accIds.add(c.accountId);
    }
    
    for(Account a : [SELECT Id, Phone, Rating FROM Account WHERE Id IN :accIds]){
        for(Contact con : trigger.new){
            a.Phone=con.Phone;
            accounts.add(a);
        }
    }
    if(accounts.size()>0){
        UPDATE accounts;        
    } 
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Priyesh,

Here is the trigger which works for you scenario:
trigger ContactTrigger on Contact (before insert,before update,after insert,after update) {
    
    if(Trigger.isAfter)
    {
        Map<Id,String> accPhone =new Map<Id,String>();
        if(Trigger.isInsert||Trigger.isUpdate)
        {
		if(Trigger.isInsert )
        {
            for(Contact con:Trigger.new)
            {
			   
                if(con.Phone!=null)
                accPhone.put(con.AccountId,con.Phone);
            }
        }
         else if(Trigger.isUpdate )
         {
             for(Contact con:Trigger.new)
             {
                 if(con.Phone!=trigger.oldMap.get(c.Id).Phone && con.Phone!=null)
                 accPhone.put(con.AccountId,con.Phone);
             }
             
         }
         
         List<Account> accList=new List<Account>([Select id,Phone from Account where id in: accPhone.keySet()]);
         for(Account acc:accList)
         {
             acc.Phone=accPhone.get(acc.Id);
         }
         update accList;
        }
    }
}
Thanks
Hope this will be helpful.