Soubhik
12/21/2019 - 10:44 AM

Example 10

Update Phone

trigger updatephone on Contact (after insert,after update) {
    
    set<id> ids = new set<id>();
    List<account> acclist = new List<account>();
    
   for(contact con : trigger.new)
   {
       ids.add(con.AccountId);
       
   }
    
    Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Phone From Account Where Id In :ids]);
    
    for(contact c : trigger.new)
    {
            Account  acc   = accountMap.get(c.AccountId) ; 
        
        if(acc!=null)
        {
            acc.Phone=c.Phone;
            
            acclist.add(acc);
            
        }
    }
    
    update acclist;
}