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
Amro HabibAmro 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;
}
}
}
Best Answer chosen by Amro Habib
CharuDuttCharuDutt
Hii Amro Habib
Try Below Code
trigger UpdateChildAccountPhoneWithParent on Account (before update) {
    list<Account> lstAccUpdate = new list<Account>();
    set<Id> SetId = new set<Id>();
    string pphone;
    for(Account a : trigger.new){
        If(a.Phone != Trigger.oldMap.get(a.Id).Phone){
            SetId.add(a.Id);
            pphone = a.Phone;
        }
    }
    list<Account> lstAcc = [select id,ParentId,Phone From Account Where ParentId In :SetId];
    for(Account Acc : lstAcc){
        Acc.phone = pphone;
        lstAccUpdate.add(Acc);
    }
    update lstAccUpdate;
}
 Please Mark It As Best Answer So That It Can Help Others.
Thank You!


 

All Answers

PriyaPriya (Salesforce Developers) 

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

Amro HabibAmro Habib

No it didnt work.

 

I dont understand why do i need the id tho? i only need the phone number of the ParentAccount

CharuDuttCharuDutt
Hii Amro Habib
Try Below Code
trigger UpdateChildAccountPhoneWithParent on Account (before update) {
    list<Account> lstAccUpdate = new list<Account>();
    set<Id> SetId = new set<Id>();
    string pphone;
    for(Account a : trigger.new){
        If(a.Phone != Trigger.oldMap.get(a.Id).Phone){
            SetId.add(a.Id);
            pphone = a.Phone;
        }
    }
    list<Account> lstAcc = [select id,ParentId,Phone From Account Where ParentId In :SetId];
    for(Account Acc : lstAcc){
        Acc.phone = pphone;
        lstAccUpdate.add(Acc);
    }
    update lstAccUpdate;
}
 Please Mark It As Best Answer So That It Can Help Others.
Thank You!


 
This was selected as the best answer
PriyaPriya (Salesforce Developers) 
ID is must because based on the ID only you can update it's field. 
Lukesh KarmoreLukesh Karmore

 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