+ Start a Discussion
udayarangareddy mekalaudayarangareddy mekala 

TRIGGER UPDATE

Hi
  I wrote the trigger whenever i will update the phone field on the ACCOUNT object,respective phone field on the CONTACT object updates automatically.i posted the code below but it was not updating the phone field on CONTACT object.
trigger updatephone on Account (before update) {
 Map<id,account>accmap = new Map<id,account>();
    for(Account acc:trigger.new){
       accmap.put(acc.id,acc);
     }   
  List<contact>listcon = [SELECT id, Phone, accountid FROM contact WHERE accountid in :accmap.keyset()];
     for(Contact con :listcon){
          con.Phone = accmap.get(con.accountid).Phone;   
     }    
      update listcon;
    }
sfdc Beginnersfdc Beginner
Hi udayarangareddy mekala,

Instead of Before Update we need to Use after Update, It will Work.
 
trigger updatephone on Account (after update) {

 Map<id,account> accmap = new Map<id,account>();
 
    for(Account acc:trigger.new){
       accmap.put(acc.id,acc);
     }
        
  List<contact> listcon = [SELECT id, Phone, accountid FROM contact WHERE accountid in :accmap.keyset()];
     for(Contact con :listcon){
          con.Phone = accmap.get(con.accountid).Phone;   
     }
         
  update listcon;
  
}

If this solves your Problem, Mark it as the Best Answer.

Thanks,
SFDC Beginner
Arunkumar RArunkumar R
Hi Uday, 

You need to write a trigger on after update event. I just updated the code, try the below one,
 
trigger updatephone on Account (after update)
{
    List<contact>listcon = [SELECT id, Phone, accountid FROM contact WHERE accountid in :trigger.newMap.keyset()];
    for(Contact con :listcon)
    {
      con.Phone = trigger.newMap.get(con.accountid).Phone;   
    }    
    update listcon;
}