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
Manikanta maram 9Manikanta maram 9 

I am new to apex and im trying to update the account phone number whenever its related contact phone is updated

trigger updateaccount on Contact (after update) {
    map<id,contact> conmap = trigger.newmap;
    set<id> allconids = conmap.keySet();
    list<account> acclist = [select id,phone from account where id in: allconids];
    for (account acc : acclist){
        
       acc.Phone = conmap.get(acc.Id).phone;
    }
update acclist;
Best Answer chosen by Manikanta maram 9
Nisar799Nisar799
Hi Manikanta,

Actually you are querying account from contact ids, which will return null. So see the updated code below.
Set<Account> accountToUpdate = new Set<Account>(); // To ensure that same account is not added twice.
for(Contact con : trigger.newmap.values()){
	accountToUpdate.add(new Account(Id= con.AccountId, phone = con.phone));
}
List<Account> accountToUpdateList = new List<Account>(accountToUpdate); // converting to list as DML can be performed on List
update accountToUpdateList;

To make it more optimized you put the filter if phone is updated for the contact, then only update the account. You have to use oldmap to do this.
Best Regards
Nisar 
Happy Coding :)

All Answers

RKSalesforceRKSalesforce
Hi Manikanta,

Please use below code. I hope this will work:
trigger updateaccount on Contact (after update) {
    Map<Id, Integer> accountIdAndPhoneMap = New Map<Id, Integer>();
    Set<ID> aID = new Set<ID>();
    List<Account> acclist = new List<Account>();
    if(trigger.isupdate)
    {  
        for(Contact con:Trigger.new)
        {
            if(con.Phone != null){
                aID.add(con.AccountId);
				accountIdAndPhoneMap.put(con.AccountId, con.Phone);
            }
        }
        for(Account a:[select id, Phone, (select id, Phone from contacts) from Account where ID IN: aID])
        {
			a.Phone = accountIdAndPhoneMap.get(a.id);
            acclist.add(a);
        }
        update acclist;
    }
}

Please let me know if helped and mark as best answer for others help.

​Regards,
Ramakant
Nisar799Nisar799
Hi Manikanta,

Actually you are querying account from contact ids, which will return null. So see the updated code below.
Set<Account> accountToUpdate = new Set<Account>(); // To ensure that same account is not added twice.
for(Contact con : trigger.newmap.values()){
	accountToUpdate.add(new Account(Id= con.AccountId, phone = con.phone));
}
List<Account> accountToUpdateList = new List<Account>(accountToUpdate); // converting to list as DML can be performed on List
update accountToUpdateList;

To make it more optimized you put the filter if phone is updated for the contact, then only update the account. You have to use oldmap to do this.
Best Regards
Nisar 
Happy Coding :)
This was selected as the best answer
Manikanta maram 9Manikanta maram 9
thanks ramakant its working
Manikanta maram 9Manikanta maram 9
thanks nisar799 for letting me know about my error