You need to sign in to do that
Don't have an account?

Getting trigger error on account object.
Requirement: When a Billing Address is modified, get the new Postal Code. Then check which Contacts on the Account are outside that Postal Code. If 1 or more Contacts are outside of the Postal Code, mark Out_of_Zip as TRUE.
I wrote this trigger on account object:
trigger Q2 on Account (after update) {
List<id> aid=new list<id>();
List<Contact> c=new List<Contact>();
List<id> aidd=new list<id>();
List<account> az=new list<account>();
List<account> acfinal=new List<account>();
for(Account a1:trigger.new){
aid.add(a1.id);
}
c=[select id,MailingPostalCode,Accountid from contact where accountid in :aid];
for(Account aa:Trigger.new){
for(contact con:c){
if(con.Accountid==aa.id && con.MailingPostalCode!=aa.BillingPostalCode){
aidd.add(aa.id);
}
}
}
az=[select id, Out_of_Zip__c from account where id in :aid];
for(account al:az){
al.Out_of_Zip__c=true;
acfinal.add(al);
}
update acfinal;
}
When I am changin the postal code on the account object, It's giving the below error:
Q2: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0012v00003EsSb5AAF; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Q2: maximum trigger depth exceeded Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate: [] Trigger.Q2: line 27, column 1
Kindly let me know why is this happening? I am a beginner to coding.
I wrote this trigger on account object:
trigger Q2 on Account (after update) {
List<id> aid=new list<id>();
List<Contact> c=new List<Contact>();
List<id> aidd=new list<id>();
List<account> az=new list<account>();
List<account> acfinal=new List<account>();
for(Account a1:trigger.new){
aid.add(a1.id);
}
c=[select id,MailingPostalCode,Accountid from contact where accountid in :aid];
for(Account aa:Trigger.new){
for(contact con:c){
if(con.Accountid==aa.id && con.MailingPostalCode!=aa.BillingPostalCode){
aidd.add(aa.id);
}
}
}
az=[select id, Out_of_Zip__c from account where id in :aid];
for(account al:az){
al.Out_of_Zip__c=true;
acfinal.add(al);
}
update acfinal;
}
When I am changin the postal code on the account object, It's giving the below error:
Q2: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0012v00003EsSb5AAF; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Q2: maximum trigger depth exceeded Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate: [] Trigger.Q2: line 27, column 1
Kindly let me know why is this happening? I am a beginner to coding.
Hi Shivis,
Please try this code.
Please let me know it is working or not??
Please mark it as Best Answer
Thank you
All Answers
Hi Shivis,
Please let me know it is working or not?Please find the solution. Getting trigger error on account object.
When you are writing a trigger on Any Objects(like Account) then at the same time you can't update the Object(Account)
Try this below code it is working fine.
Please mark it as Best Answer so that other people would take reference from it.
Thank You
Shivi,
You are getting this error "execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0012v00003EsSb5AAF; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Q2: maximum trigger depth exceeded Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate Account trigger event AfterUpdate: [] Trigger.Q2: line 27, column 1
Because you have written trigger on Account update and again you are updating account.
If we want to update the account(before update) then you have to update in trigger.new instead of List<account> acfinal=new List<account>();
I completely understood the reason why recurssion is arising due to update trigger again updating accounts. How can I resolve that? because we do have to update account records(Out_of_Zip__c field)
In your first answer, what you have done with the help of map, i guess I have done the same with the help of list, i guess the main problem is in update part. Correct me if I am wrong. Also, let me know how are you planing to place a DML statement in ur first answer?
Hi Shivis,
Please try this code.
Please let me know it is working or not??
Please mark it as Best Answer
Thank you