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
adi salesforceadi salesforce 

Trigger to update account related contacts

Account 'Abc' has 3 contacts. In two contact records phone number is empty  but in third contact phone number is not empty. The third contact phone number should be updated in other two records phone numbers.
Raj VakatiRaj Vakati
Try this
 
trigger UpdateContactPhone on Contact (before insert) {
    Map<Id,String> accIds = new Map<Id,String>();
    for(Contact con : Trigger.new) {  
        If(con.Phone!=null){
            accIds.put(con.AccountId , con.Phone) ; 
        }
        
    }
    
    List<Contact> cons = [Select Id ,Phone,AccountId from Contact where AccountId IN :accIds.keySet() AND Phone=NULL ];
    for(Contact c :cons){
        c.Phone = accIds.get(c.AccountId); 
    }
    
    update cons ;
    
}