You need to sign in to do that
Don't have an account?
Chuck M Rohllf
maximum trigger depth exceeded Account trigger event AfterUpdate Contact trigger event AfterUpdate
I have a trigger on Account that will update all related Contacts when updated. I'm receiving the following error: maximum trigger depth exceeded Account trigger event AfterUpdate Contact trigger event AfterUpdate Any guidance would be appreciated. Here is my trigger:
trigger UpdateRelatedContacts on Account (after update){ List<Contact> contactList = new List<Contact>(); List<Contact> contactList1 = new List<Contact>(); for(Account acc: [SELECT Id,RecordTypeID,(SELECT Id,RecordTypeID FROM Contacts) FROM Account WHERE Id in: Trigger.new]){ If(acc.Contacts.size()>0 && (acc.RecordTypeID =='012E0000000PR3u' || acc.RecordTypeID =='012440000002T4h')){ contactList.addALL(acc.Contacts); } } for(contact c:contactList){ If(c.RecordTypeID !='012440000002T6J'){ c.checkbox__c=true; contactList1.add(c); } update contactList1;
Well, the easiest way to break this loop would probably be to check whether you actually need to update your contacts and accounts before you update them. For example
Is this necessary? What if 'of_Sync_to_Marketo_Contacts__c' already has the right value? If you did a check first then you'd avoid pointlessly firing DML operations that did nothing, and avoid firing triggers as well. Same thing here:
You could easily check the state of that checkbox and avoid pointless trigger calls
Lastly, this code is ... well... not great. Lots of things you could clean up
Much simpler, a single SOQL call that does all the filtering and gives you back only the results you actually need, then you simply set the one field you care about and update that list. Although having fields like 'checkbox__c' is a terrible idea. Nobody will ever know what that field is for, give it a proper name that describes it at least a little bit.
Although frankly it would be much easier to just create a rollup summary field that counts how many contacts have Sync_with_Marketo__c set to true. It would dynamically update on its own without any triggers. https://help.salesforce.com/articleView?id=000249282&type=1 is almost exactly what you need, except you'd have to add 1 criteria.In your Contact Trigger you are blindly updating the Accounts in after update..
So try to add one more condition in the After Update Trigger:
Trigger CountContacts on Contact (after insert, after update, after delete) {
Set<Id> AccountIds = new Set<Id>();
Map<Id,Account> Summary = new Map<Id,Account>();
if(Trigger.isInsert || Trigger.isDelete)
{
for (Contact c : (Trigger.new<>null ? Trigger.new : Trigger.old)){
if (c.Sync_with_Marketo__c && c.AccountId <> null){
AccountIds.add(c.AccountId);
Summary.put(c.AccountId,new Account(id=c.AccountId,of_Sync_to_Marketo_Contacts__c=0));
}
}
}
else if(Trigger.isUpdate)
{
for (Contact c : Trigger.new){
if (c.Sync_with_Marketo__c && c.AccountId <> null && c.AccountId != Trigger.oldMap.get(c.Id).AccountId){
AccountIds.add(c.AccountId);
Summary.put(c.AccountId,new Account(id=c.AccountId,of_Sync_to_Marketo_Contacts__c=0));
// The below logic you are completely missed.
// The below If loop will reduce the 1 contact count from the Old Account whilee you are updating the Contact
// with New Account.
if(Trigger.oldMap.get(c.Id).AccountId <> null)
{
Id oldAccId = Trigger.oldMap.get(c.Id).AccountId;
AccountIds.add(oldAccId);
Summary.put(oldAccId,new Account(id=oldAccId,of_Sync_to_Marketo_Contacts__c=0));
}
}
}
}
if (!AccountIds.isEmpty())
{
for (AggregateResult aggr : [select count(id),AccountId FROM Contact WHERE Sync_with_Marketo__c=true AND AccountId IN : AccountIds GROUP BY AccountId])
{
Account a = Summary.get((Id)aggr.get('AccountId'));
a.of_Sync_to_Marketo_Contacts__c = (Decimal)aggr.get('expr0');
Summary.put(a.Id,a);
}
update Summary.values();
}
}
===========
And one more suggesition is add the boolean in the Account Trigger to stop the Unwanted Recurssion
1. Create the Below Apex Class in your Org:
public class RecursiveHandler
{
public static Boolean IsNotRecursive = true;
}
2. Call the above static boolean in your Account Trigger:
trigger UpdateRelatedContacts on Account (after update){
List<Contact> contactList = new List<Contact>();
List<Contact> contactList1 = new List<Contact>();
if(RecursiveHandler.IsNotRecursive){
RecursiveHandler.IsNotRecursive = false;
for(Account acc: [SELECT Id,RecordTypeID,(SELECT Id,RecordTypeID FROM Contacts) FROM Account WHERE Id in: Trigger.new]){
If(acc.Contacts.size()>0 && (acc.RecordTypeID =='012E0000000PR3u' || acc.RecordTypeID =='012440000002T4h')){
contactList.addALL(acc.Contacts);
}
}
for(contact c:contactList){
If(c.RecordTypeID !='012440000002T6J'){
c.checkbox__c=true;
contactList1.add(c);
}
}
if(contactList1.size() > 0)
update contactList1;
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C