You need to sign in to do that
Don't have an account?
Nandini
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.
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.
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:
Trigger:
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
Apex Class with Static Variable
public class ContactTriggerHandler { public static Boolean isFirstTime = true; }
Trigger Code
Let us know if this will help you
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