You need to sign in to do that
Don't have an account?
Amro Habib
accounts trigger
so im trying to update all acoounts phone numbers, only when their Parent Account phone number is updated, but its not working, and my loop doesnt even iterate for some reason? whats wrong with code?
trigger UpdatePhoneNum on Account (before update) {
for(Account a : trigger.new)
{
if(String.isNotBlank(a.ParentId)){
Account acc = [SELECT Phone FROM Account WHERE Id = :a.ParentId LIMIT 1];
a.Phone = acc.Phone;
}
}
}
trigger UpdatePhoneNum on Account (before update) {
for(Account a : trigger.new)
{
if(String.isNotBlank(a.ParentId)){
Account acc = [SELECT Phone FROM Account WHERE Id = :a.ParentId LIMIT 1];
a.Phone = acc.Phone;
}
}
}
Try Below Code Please Mark It As Best Answer So That It Can Help Others.
Thank You!
All Answers
Hi Amro,
You are not fetching the account id in the SOQL.
Account acc = [SELECT ID, Phone FROM Account WHERE Id = :a.ParentId LIMIT 1];
And this will work now. Please try.
If it resolve, please mark it as best answer so that it can help others.
regards,
Priya Ranjan
No it didnt work.
I dont understand why do i need the id tho? i only need the phone number of the ParentAccount
Try Below Code Please Mark It As Best Answer So That It Can Help Others.
Thank You!
Hii Amro Habib , Try below code and If it resolve, please mark it as best answer so that it can help others.
trigger UpdateAccountPhoneWithParentPhone on Account ( after update) {
set<id> AccIds=new set<id>();
string phoneField;
for(Account acc:trigger.new){
if(acc.phone!=Trigger.oldMap.get(acc.Id).phone){
AccIds.add(acc.Id);
phoneField=string.valueOf(acc.phone);
}
}
list<account> acclist=[SELECT phone FROM Account WHERE ParentId IN :AccIds ];
for(Account u:acclist){
u.phone=phoneField;
}
update acclist;
}
Thanks