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
Aman Mishra 01Aman Mishra 01 

Write a Trigger :-> {Please help me to solve this Problem}

Whenever I update the Account Phone , Then please,  update it's Related Contact Phone Number If  related Contact Exist .                                    
Best Answer chosen by Aman Mishra 01
Suraj Tripathi 47Suraj Tripathi 47
Hi,
Please try the below code -
Apex Trigger Code:
trigger updatePhone on Account (after update) {
    Set <Id> accountIdSet = new Set <Id>();
    Map <Id,String> accountMap = new Map <Id,string> ();
    for(Account acc:trigger.new) {
        if(Trigger.oldMap.get(acc.Id).phone != acc.phone) {
            accountIdSet.add(acc.id);
        }
        if(acc.phone != null) {
            accountMap.put(acc.id,acc.phone);
        }
    }
    List <contact> contactList = new List <Contact>();
    for(Contact con:[select id,accountId,Phone from contact where accountId in:accountIdSet]) {
        if(accountMap != null && accountMap.containsKey(con.accountId)) {
            con.phone = accountMap.get(con.accountId);
            contactList.add(con);
        }
    }
    if(contactList.size() >0) {
        update contactList;
    }

}
Output: Go to any Account and update the phone number. The phone number in its related contacts automatically gets updated.

I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.

Thanks and Regards,
Suraj Tripathi

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi,
Please try the below code -
Apex Trigger Code:
trigger updatePhone on Account (after update) {
    Set <Id> accountIdSet = new Set <Id>();
    Map <Id,String> accountMap = new Map <Id,string> ();
    for(Account acc:trigger.new) {
        if(Trigger.oldMap.get(acc.Id).phone != acc.phone) {
            accountIdSet.add(acc.id);
        }
        if(acc.phone != null) {
            accountMap.put(acc.id,acc.phone);
        }
    }
    List <contact> contactList = new List <Contact>();
    for(Contact con:[select id,accountId,Phone from contact where accountId in:accountIdSet]) {
        if(accountMap != null && accountMap.containsKey(con.accountId)) {
            con.phone = accountMap.get(con.accountId);
            contactList.add(con);
        }
    }
    if(contactList.size() >0) {
        update contactList;
    }

}
Output: Go to any Account and update the phone number. The phone number in its related contacts automatically gets updated.

I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.

Thanks and Regards,
Suraj Tripathi
This was selected as the best answer
ravi soniravi soni
hi Aman,
try below trigger.
trigger updateContactPhone on Account (after update) {
     Map<string,Account> idWiseRecords = new Map<string,Account>();
    list<Contact> lstContact = new list<Contact>();
    if(trigger.isAfter && trigger.isUpdate){
       
       
        for(Account acc : trigger.new){
                idWiseRecords.put(acc.Id,acc);
            }
        
        for(Contact con : [SELECT Id,Phone,AccountId FROM Contact Where AccountId In : idWiseRecords.keyset()]){
            if(idWiseRecords.containsKey(con.AccountId)){
         con.Phone = idWiseRecords.get(con.AccountId).Phone;            
            }
            lstContact.add(con);
        }
        if(lstContact.size() > 0){
            update lstContact;
        }
    }

}

don't forget to mark it as best answer.
Thank you
Antriksh jainAntriksh jain
Hi,
please try below code

trigger updateContact on Account (after update) {
    List <contact> contactList = new List <Contact>();
    Set <Id> accountIdSet = new Set <Id>();
    Map <Id,String> accountMap = new Map <Id,string> ();
    for(Account acc:trigger.new) {
        if(acc.phone != null && Trigger.oldMap.get(acc.Id).phone != acc.phone) {
            accountIdSet.add(acc.id);
        }
        
        List<Contact>conList= [select id,accountId,Phone from contact where accountId in:accountIdSet];
    
    for(Contact con:conList) {
        if(accountMap != null && accountMap.containsKey(con.accountId)) {
            con.phone = accountMap.get(con.accountId);
            contactList.add(con);
        }
    }
    if(contactList.size() >0) {
        update contactList;
    }
    }
}