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
maheswar reddy 36maheswar reddy 36 

write a trigger to update contact phone and also update account phone number?

Best Answer chosen by maheswar reddy 36
Abdul KhatriAbdul Khatri
Here is the improved version of my earlier code

optimized, efficient, less number of lines and error less.
  • Took care of null values
  • Only works when there is a change in the phone or phone available
  • Work on both Insert and update Contact
  • Test Class below with 100% Coverage
trigger UpdateAccountPhone on Contact (after insert, after update) {
    
    Map<Id, Contact> idAccountContactMap = new Map<Id, Contact>();
    
    for(Contact contact : trigger.new) {
        
        if(contact.AccountId == null) continue;

        if(contact.Phone == null || (trigger.isUpdate && contact.Phone == trigger.oldMap.get(contact.Id).Phone)) continue;
        
        idAccountContactMap.put(contact.AccountId, contact);
    }

    if(idAccountContactMap.isEmpty()) return;
    
    List<Account> accountToUpdateList = [Select Phone From Account Where Id IN :idAccountContactMap.keySet()];
    
    for(Account account : accountToUpdateList) {
        account.Phone = idAccountContactMap.get(account.Id).Phone;
    }
    
    update accountToUpdateList;
}
Test Class
 
@isTest
public class UpdateAccountPhoneTest {
    
    static testmethod void testUpdateAccountPhoneOnInsert () {
        
        Account account = new Account (Name = 'Test');
        insert account;
        
        Contact contact = new Contact (LastName = 'Test', Phone='2341111111', AccountId = account.Id);
        insert contact;
        
        System.assert([Select Phone From Account Where Name = 'Test'].Phone == '2341111111');
    }

    static testmethod void testUpdateAccountPhoneOnInsertNullcheck () {
        
        Account account = new Account (Name = 'Test');
        insert account;
        
        Contact contact = new Contact (LastName = 'Test', AccountId = account.Id);
        insert contact;
        
        System.assert([Select Phone From Account Where Name = 'Test'].Phone == null);
    }
    
    static testmethod void testUpdateAccountPhoneOnUpdate () {
        
        Account account = new Account (Name = 'Test');
        insert account;
        
        Contact contact = new Contact (LastName = 'Test', AccountId = account.Id);
        insert contact;
        
        contact.Phone = '2341111111';
        update contact;
        
        System.assert([Select Phone From Account Where Name = 'Test'].Phone == '2341111111');
    }    

}


Please use and let me know how it goes

All Answers

Abdul KhatriAbdul Khatri
Here is the code
trigger UpdateAccountPhone on Contact (after update) {
    
    Map<Id, Contact> idAccountContactMap = new Map<Id, Contact>();
    
    for(Contact contact : trigger.new) {
        
        if(contact.AccountId == null) continue;
        
        idAccountContactMap.put(contact.AccountId, contact);
    }

    if(idAccountContactMap.isEmpty()) return;
    
    List<Account> accountToUpdateList = [Select Phone From Account Where Id IN :idAccountContactMap.keySet()];
    
    for(Account account : accountToUpdateList) {
        account.Phone = idAccountContactMap.get(account.Id).Phone;
    }
    
    update accountToUpdateList;
}

 
Ajay K DubediAjay K Dubedi
Hi Maheswar,

This is a simple and easy code to understand for new salesforce developer.

trigger UpdateAccountPhoneNumberTrigger on Contact (before update) {

     Set<id> IdCollect = new Set<id>();
        For(Contact con :Trigger.New){
            If (con.Phone != null){
                IdCollect.add(con.AccountId);
            }
        }
        List<Account> acList = new List<Account>();
        acList =[Select Id,Phone from Account Where Id IN:IdCollect];
    
        //using simple map to put contact phone number
        Map <Id, string> conmap = new Map <Id, string>();
        for(Contact c : trigger.New){
            conmap.put(c.AccountId , c.Phone);
        }
       if(acList!=null){
                for(Account ac : acList ){
                        ac.Phone = conmap.get(ac.Id);
            }
        }
        if(acList.size()>0){
            Update acList;
        } 
    }
    
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Abdul KhatriAbdul Khatri
Here is the improved version of my earlier code

optimized, efficient, less number of lines and error less.
  • Took care of null values
  • Only works when there is a change in the phone or phone available
  • Work on both Insert and update Contact
  • Test Class below with 100% Coverage
trigger UpdateAccountPhone on Contact (after insert, after update) {
    
    Map<Id, Contact> idAccountContactMap = new Map<Id, Contact>();
    
    for(Contact contact : trigger.new) {
        
        if(contact.AccountId == null) continue;

        if(contact.Phone == null || (trigger.isUpdate && contact.Phone == trigger.oldMap.get(contact.Id).Phone)) continue;
        
        idAccountContactMap.put(contact.AccountId, contact);
    }

    if(idAccountContactMap.isEmpty()) return;
    
    List<Account> accountToUpdateList = [Select Phone From Account Where Id IN :idAccountContactMap.keySet()];
    
    for(Account account : accountToUpdateList) {
        account.Phone = idAccountContactMap.get(account.Id).Phone;
    }
    
    update accountToUpdateList;
}
Test Class
 
@isTest
public class UpdateAccountPhoneTest {
    
    static testmethod void testUpdateAccountPhoneOnInsert () {
        
        Account account = new Account (Name = 'Test');
        insert account;
        
        Contact contact = new Contact (LastName = 'Test', Phone='2341111111', AccountId = account.Id);
        insert contact;
        
        System.assert([Select Phone From Account Where Name = 'Test'].Phone == '2341111111');
    }

    static testmethod void testUpdateAccountPhoneOnInsertNullcheck () {
        
        Account account = new Account (Name = 'Test');
        insert account;
        
        Contact contact = new Contact (LastName = 'Test', AccountId = account.Id);
        insert contact;
        
        System.assert([Select Phone From Account Where Name = 'Test'].Phone == null);
    }
    
    static testmethod void testUpdateAccountPhoneOnUpdate () {
        
        Account account = new Account (Name = 'Test');
        insert account;
        
        Contact contact = new Contact (LastName = 'Test', AccountId = account.Id);
        insert contact;
        
        contact.Phone = '2341111111';
        update contact;
        
        System.assert([Select Phone From Account Where Name = 'Test'].Phone == '2341111111');
    }    

}


Please use and let me know how it goes
This was selected as the best answer
Abdul KhatriAbdul Khatri
Hi, Any feedback.