You need to sign in to do that
Don't have an account?
Manikanta 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;
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;
Actually you are querying account from contact ids, which will return null. So see the updated code below.
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
Please use below code. I hope this will work:
Please let me know if helped and mark as best answer for others help.
Regards,
Ramakant
Actually you are querying account from contact ids, which will return null. So see the updated code below.
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 :)