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
Deepu sfdcDeepu sfdc 

update contact phone field

Hi,

whenever account phone is updated i want to update the contact phone field too.
How can we achieve this with triggers..
can you send me the trigger code

Thanks in Advance 

NagendraNagendra (Salesforce Developers) 
Hi Deepu,

Try this and let us know if it works.
trigger updatephonefield on Contact (after update, after insert ) {
 for (Contact cp : Trigger.new) {
            cp.Phone == Account.Phone
            }
}
Thanks,
Nagendra
 
Deepu sfdcDeepu sfdc

Hi nagendra

this trigger is on contact object ... i want to update the contact field when ever we update the account field

BTW im getting this error with this code

Error: Compile Error: Comparison arguments must be compatible types: String, Schema.SObjectField at line 3 column 13

Deepali KulshresthaDeepali Kulshrestha
Hi Deepu,

Please try this code if you want to update contact Phone field if account phone field is updated.

trigger AccountFieldUpdateTrigger on Account (After Update) {
    //MAP that will hold the contacts related to an account...
    Map<Id,List<Contact>> conMap=new Map<Id,List<Contact>>();
    List<Contact> conList=[select name,accountId,phone from Contact where accountId in:Trigger.New];
    
    for(Contact c:conList){
        
        if(!conMap.containsKey(c.accountId)){
            List<Contact>  cList=new List<Contact>();
            cList.add(c);
            conMap.put(c.accountId,cList);
        }
        else{
            List<Contact>  cList= conMap.get(c.AccountId);
            CList.add(c);
            conMap.put(c.accountId,cList);
            
        }
    }
    
    List<Contact>  toupdateConList=new List<Contact>();
    
    for( Id accountId : Trigger.newMap.keySet() )
    {    //To check whether account phone field is updated...
        if( Trigger.oldMap.get( accountId ).Phone != Trigger.newMap.get( accountId ).Phone )
        {
            for(Contact c: conMap.get(accountId)) {
                c.Phone=Trigger.newMap.get( accountId ).Phone;
                toupdateConList.add(c);
            }
        }
    }
    if(toupdateConList.size()>0){ 
        update toupdateConList;
    }
    
}

Please mark it as best answer if it helps you.

Thanks.
Deepali Kulshrestha
TruptiTrupti
Why to go for code if you can achieve same with out of the box functionality.
You can do this using process builder.

Thanks
Raj VakatiRaj Vakati
Here is te code
 
trigger updatephonefield on Account (after update, after insert ) {
 Map<Id,Account> accMap = Trigger.oldMap ; 
 Map<Id,String> newPhne =  Map<Id,String>();
 for (Account cp : Trigger.new) {
	 if(cp.Phone!=accMap.get(cp.Id).Phone){
		newPhne.add(cp.Id ,cp.Phone); 
		 
	 }
	 
  }
  
List<Contact> cons = [Select Id ,Phone , AccountId from COntact where AccountId IN :newPhne.keySet()];

for(Contact c : cons){
	c.Phone = newPhne.get(c.AccountId);
}
  
  update c;
}