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
SFDummySFDummy 

Maximum trigger depth exceeded - Account Trigger/Opportunity Trigger

I am getting maximum trigger dept exceeded error, because (1) Account After update trigger is updating opportunities (used in SF2SF updates)   (2) Opportunity After Update trigger is updating account information (internal sales process)

I used to have #2 scenario in workflow rules, that I have to move to trigger due to WF rule limit. I am using static variable but I am still getting this error. How can I identify/differentiate #1 and #2 scenario in Trigger and prevent this recursive calling.  Any suggestions?

 
Best Answer chosen by SFDummy
SKolakanSKolakan
See this article on avoiding recursive trigger: https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

For long term, it is better to adopt a trigger handler framework. Here is a nice article on the topic: https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices



 

All Answers

SKolakanSKolakan
See this article on avoiding recursive trigger: https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

For long term, it is better to adopt a trigger handler framework. Here is a nice article on the topic: https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices



 
This was selected as the best answer
J CesarJ Cesar
Write a simple Apex class to check for recursion and then invoke it in the start of the code every time you write a trigger that may cause recursive calls.
You can easily find some examples online if unsure.
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to stop recursive trigger
1) http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

 Apex Class with Static Variable

public class ContactTriggerHandler {
public static Boolean isFirstTime = true;
}

Trigger Code

trigger ContactTriggers on Contact (after update)
{
Set<String> accIdSet = new Set<String>();
if(ContactTriggerHandler.isFirstTime)
{
ContactTriggerHandler.isFirstTime = false;
for(Contact conObj : Trigger.New)
{
if(conObj.name != 'Test') { accIdSet.add(conObj.accountId); }
} // any code here
}
}

Let us know if this will help you