+ Start a Discussion
Lakshmi SLakshmi S 

Recursive trigger ?

Hi Team,

Please advise below trigger format is correct or not, we are avoiding recursive triggers using static methods in helper class.
trigger testTrigger on Account(before insert, before update, after insert){
        if(Trigger.isInsert){
            if(Trigger.isAfter){
           TriggerHandlerCls.testMethod(Trigger.New);
          }
    }

}

class:
---------
public class TriggerHandlerCls{

public static void testMethod(List<Account> acc){
// logic

}

}
Please let me know.

Thanks,
Lakshmi
 
Best Answer chosen by Lakshmi S
Steven NsubugaSteven Nsubuga
Here
trigger testTrigger on Account(before insert, before update, after insert){
	
		if(Trigger.isInsert){
            if(Trigger.isAfter){
				if(TriggerHandlerCls.firstRun){
					TriggerHandlerCls.firstRun = false;
					TriggerHandlerCls.testMethod(Trigger.New);
				}
				if(TriggerHandlerCls2.firstRun){
					TriggerHandlerCls2.firstRun = false;
					TriggerHandlerCls2.testMethod(Trigger.New);
				}
            }
        }
	
}

public class TriggerHandlerCls{
	
	public static boolean firstRun = true;
	
	public static void testMethod(List<Account> acc){
	// logic

	}

}
public class TriggerHandlerCls2{
	
	public static boolean firstRun = true;
	
	public static void testMethod(List<Account> acc){
	// logic

	}

}

 

All Answers

Steven NsubugaSteven Nsubuga
trigger testTrigger on Account(before insert, before update, after insert){
	if(TriggerHandlerCls.firstRun){
		TriggerHandlerCls.firstRun = false;
		if(Trigger.isInsert){
            if(Trigger.isAfter){
				TriggerHandlerCls.testMethod(Trigger.New);
            }
        }
	}
}
 
public class TriggerHandlerCls{
	
	public static boolean firstRun = true;
	
	public static void testMethod(List<Account> acc){
	// logic

	}

}

 
Lakshmi SLakshmi S
Hi Steven Nsubuga,

Can we use same static variable in multiple handler classes.
public static boolean firstRun = true;

Please let me know.

Thanks,
Lakshmi.
Steven NsubugaSteven Nsubuga
Each handler class must have its own static variable.
Lakshmi SLakshmi S
Hi Steven Nsubuga,

If we have multiple handler classes after insert, how can we achieve that, please advise.

Thanks,
Lakshmi.
Steven NsubugaSteven Nsubuga
I suggest you have a top level class that has the static variable, then depending on that 1 variable, other handler classes can be called.
Lakshmi SLakshmi S
Hi Steven Nsubuga,

Thanks for your response.
I have two handler classes and both are performing after insert only, how can we call without recursive, please advise.
 
trigger testTrigger on Account(before insert, before update, after insert){
	
		if(Trigger.isInsert){
            if(Trigger.isAfter){
				TriggerHandlerCls.testMethod(Trigger.New);
                                TriggerHandlerCls2.testMethod(Trigger.New);
            }
        }
	
}

public class TriggerHandlerCls{
	
	public static boolean firstRun = true;
	
	public static void testMethod(List<Account> acc){
	// logic

	}

}
public class TriggerHandlerCls2{
	
	public static boolean firstRun = true;
	
	public static void testMethod(List<Account> acc){
	// logic

	}

}

Thanks,
Lakshmi.
 
Steven NsubugaSteven Nsubuga
Here
trigger testTrigger on Account(before insert, before update, after insert){
	
		if(Trigger.isInsert){
            if(Trigger.isAfter){
				if(TriggerHandlerCls.firstRun){
					TriggerHandlerCls.firstRun = false;
					TriggerHandlerCls.testMethod(Trigger.New);
				}
				if(TriggerHandlerCls2.firstRun){
					TriggerHandlerCls2.firstRun = false;
					TriggerHandlerCls2.testMethod(Trigger.New);
				}
            }
        }
	
}

public class TriggerHandlerCls{
	
	public static boolean firstRun = true;
	
	public static void testMethod(List<Account> acc){
	// logic

	}

}
public class TriggerHandlerCls2{
	
	public static boolean firstRun = true;
	
	public static void testMethod(List<Account> acc){
	// logic

	}

}

 
This was selected as the best answer
Lakshmi SLakshmi S
Hi Steven Nsubuga,

Thanks for you quick reply.

Thanks,
Lakshmi.
Lakshmi SLakshmi S
Hi Steven Nsubuga,

I have a issue with field update using trigger.
Requirement is updating field A based on Field B, we are getting field B value using workflow, based on field B we are updating field A using before insert and update trigger. if i am not restrict recursive it's working fine. In case if i am using Static boolean variable the field is not updated. How can we resolve that issue ?
Please let me know.

Thanks
Lakshmi.