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
ShiviSShiviS 

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.
Best Answer chosen by ShiviS
Suraj Tripathi 47Suraj Tripathi 47

Hi Shivis,

Please try this code.

if(trigger.isBefore && trigger.isUpdate){
        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){
                   aa.Out_of_Zip__c =true;
                }
            }
        }
    }

Please let me know it is working or not??
Please mark it as Best Answer

Thank you

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi Shivis,

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.

if(trigger.isBefore && trigger.isUpdate){
        set<Id> accountSet=new set<Id>();
        for(Account ac:trigger.new){
            accountSet.add(ac.Id);
        }
        Map<Id,Contact> contactMap=new Map<Id,Contact>([select id,MailingPostalCode,accountid from Contact where accountid in:accountSet ]);
         for(Account ac:trigger.new){
            for(Id id1:contactMap.keySet()){
                if(ac.id==contactMap.get(id1).accountid && ac.BillingPostalCode!=contactMap.get(id1).MailingPostalCode){
                      ac.Out_of_Zip__c =true;
                }
            }
        }    ​
   ​
Please let me know it is working or not?

Please mark it as Best Answer so that other people would take reference from it.

Thank You
Suraj Tripathi 47Suraj Tripathi 47

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>();

ShiviSShiviS
Hi Suraj, thanks for replying!

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?
Suraj Tripathi 47Suraj Tripathi 47

Hi Shivis,

Please try this code.

if(trigger.isBefore && trigger.isUpdate){
        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){
                   aa.Out_of_Zip__c =true;
                }
            }
        }
    }

Please let me know it is working or not??
Please mark it as Best Answer

Thank you

This was selected as the best answer
ShiviSShiviS
Yes, it's working. Thanks! I will try to understand it.
alt manalt man
Sapendo che questi giganti dei social media possono curiosare nelle nostre vite private, molte persone stanno scegliendo l'opzione di abbandonare la nave ed eliminare completamente la loro presenza sui social media. Ottieni i passaggi su eliminare definitivamente account instagram qui. https://migliorguida.net/eliminare-account-instagram/
Baya AdamBaya Adam
My Articles is a writer’s community where writers can share their stories all over the world. Signup and share your stories to all over the world. Follow your favorite writers, create groups, forums, chat, and much much more!

 
jesszi12 joejesszi12 joe
Great Post! Thank you for visiting here. https://nextgen-patientportal.com/