0

showing error while updating Account record.

Apex Trigger code:

trigger contactUpdate2 on Account (after update) {

list<contact> conlist=new list<contact>();
map<id,Account> accountIds=new map<id,account>();

for(Account acc:trigger.new)
{
    accountIds.put(acc.id, acc);
}
Map<id, Account> accList=new Map<id, Account> ([select id, phone,(select id, phone from contacts) from account where id in:accountIds.keyset()]);

for(account a:trigger.new)
{
    list<contact> con=accList.get(a.id).contacts;
    for(contact cc:con)
    {
        cc.Phone=a.phone;
        conList.add(cc);
    }
}
Update conList;

}

Error:

Error:Apex trigger contactUpdate2 caused an unexpected exception, contact your administrator: contactUpdate2: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0030K00001LJjz2QAD; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccount: maximum trigger depth exceeded Account trigger event AfterUpdate for [0010K00001bpmmX] Contact trigger event AfterUpdate for [0030K00001LJjz2, 0030K00001LJk1S] Account trigger event AfterUpdate for [0010K00001bpmmX] Contact trigger event AfterUpdate for [0030K00001LJjz2, 0030K00001LJk1S] Account trigger event AfterUpdate for [0010K00001bpmmX] Contact trigger event AfterUpdate for [0030K00001LJjz2, 0030K00001LJk1S] Account trigger event AfterUpdate for [0010K00001bpmmX] Contact trigger event AfterUpdate for [0030K00001LJjz2, 0030K00001LJk1S] Account trigger event AfterUpdate for [0010K00001bpmmX] Contact trigger event AfterUpdate for [0030K00001LJjz2, 0030K00001LJk1S] Account trigger event AfterUpdate for [0010K00001bpmmX] Contact trigger event AfterUpdate for [0030K00001LJjz2, 0030K00001LJk1S] Account trigger event AfterUpdate for [0010K00001bpmmX] Contact trigger event AfterUpdate for [0030K00001LJjz2, 0030K00001LJk1S] Account trigger event AfterUpdate for [0010K00001bpmmX] Contact trigger event AfterUpdate for [0030K00001LJjz2, 0030K00001LJk1S]: []: Trigger.contactUpdate2: line 20, column 1

1

//Helper class

trigger ContactPhoneUpdate on Account (after Update) {
if(trigger.isUpdate && trigger.isAfter){
    accountPhoneUpdate.accountMethod(trigger.new,trigger.oldmap); }}

//class: accountPhoneUpdate

public class accountPhoneUpdate {
public static void accountMethod(List<Account>aclist,map<id,account> oldmap){
    set<id> accountId=new set<id>();
    for(Account ac:aclist){
        account accountOld=oldmap.get(ac.id);
        if(ac.Phone!=accountOld.phone){
            accountId.add(ac.Id);
        }
    }

    if(accountId.size()>0){
        map<id,account> accountmap=new map<id,account>([select id,phone,(select id,phone from contacts) from account where id in:accountId]);
        List<contact> clist=new List<contact>();
        for(account ac:aclist){
            account accountOld=oldmap.get(ac.id);
            if(ac.Phone!=accountOld.phone){
                if(accountmap.containskey(ac.id)){
                    account ac1=accountmap.get(ac.id);
                    List<contact>lstCont = ac1.contacts;
                    for(contact c:lstCont){
                        c.phone=ac.phone;
                        clist.add(c);
                    }

                }
            }
        }
        if(!clist.isEmpty()){
            update clist;
        }

    }
}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.