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
NandiniNandini 

Recursive trigger issue ?

Hi Team,

For before event (before insert or update) triggers on same object need to use static boolean variable in helper classes. Is it mandatory for recusive triggers ? or Not ?

Thanks,
sfdc team.
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

I trust you are doing very well.

Before insert and update triggers will not cause any recursion. But it might possible that there's workflow on the object that can cause a field update. That will, in turn, trigger the update trigger.
A recursive trigger is one that is called over and over. It can lead to the infinite loop and which can result to governor limit sometime. Sometimes it can also result in unexpected output. If not controlled will result in this error: maximum trigger depth exceeded. So we should write code in such a way that it does not result to recursion.

To avoid recursion we can use a public class static variable. As per the order of execution, all Before Trigger and After Trigger will fire before the workflow update. To prevent Trigger to be fired the second time, after workflow field update we can set a static boolean variable in a Class.

Create a static variable in a class as true. Make it false before or after a trigger has been executed. Variable will become false when trigger runs for the first time (before workflow update). If the variable is true only then trigger will fire and if the trigger will try to execute the second time then it will not fire.

Handler Class:
public class TestHandler{
     public static Boolean isTriggerExecuted = true;
}

Trigger:
trigger testTrigger on Account (before update){
     
    if(TestHandler.isTriggerExecuted){
         
        //Logic

        TestHandler.isTriggerExecuted = false;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
Amit Chaudhary 8Amit Chaudhary 8
Yes, you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.


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

 
Ajay K DubediAjay K Dubedi
Hi Nandu,

Below Sample code can fullfill your requirements. Hope this will work for you.

Trigger :

trigger CourseTrig on Course__c (after insert, after update) { 
if(trigger.isAfter && trigger.isInsert || trigger.isAfter && trigger.isUpdate)
{
CheckProfCourses.notMoreThanFour(trigger.New);

if(trigger.isAfter && trigger.isInsert)
{
CloneCourse.courseBackForm(trigger.New);

}


Class :

public class CloneCourse {

private static boolean firstRun = true;
public static boolean isFirstRun(){
if(firstRun)
{
firstRun = false;
return true;
}
else
{
return firstRun;
}
}

public static void courseBackForm(List<Course__c> cList)

List<Course__c> courseNewList = new List<Course__c>(); 
try
{
if(CloneCourse.isFirstRun())
{
for(Course__c cObj : cList)
{
Course__c cLocalObj = new Course__c();
cLocalObj.Name = 'Back '+ cObj.Name ;
cLocalObj.Class__c = cObj.Class__c ;
cLocalObj.Total_Attendance_Required__c = cObj.Total_Attendance_Required__c ;
cLocalObj.Start_Date__c = cObj.Start_Date__c ;
cLocalObj.End_Date__c = cObj.End_Date__c ;
cLocalObj.Professor__c = cObj.Professor__c ; 
courseNewList.add(cLocalObj); 
}
}

if(courseNewList != null && courseNewList.size() > 0)
insert courseNewList;
}
catch(Exception e)
{
system.debug('The following error has occurred' + e.getMessage());
}
}
}

     
Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi


 
Rama ChunduRama Chundu
Using Static variable doesn't really work incase of batches. As the batches are executed in 200 records at a time, your handler class might not execute after the first 200 records are processed.