Print this page

Best Practice in avoiding Trigger Recursion

Knowledge Article Number 000199485
Handling recursion in apex triggers and the best practices related to it.
Best practice for triggers:
1. One Trigger Per Object so that you don't have to think about the execution order as there is no control over which trigger would be executed first.
2. Logic-less Triggers - use Helper classes to handle logic.
3. Code coverage 100%
4. Handle recursion - To avoid the recursion on trigger, make sure your trigger is getting executed only one time.
public Class AvoidRecursion{
    private static boolean firstRun = true;
    public static boolean isFirstRun(){
      firstRun = false;
      return true;
        return firstRun;
Handle the same in trigger :
trigger updateTrigger on anyObject(after update) {

Static in Salesforce are per transaction so the value will be true only for the current transaction and will be initialized to true for other transactions(Don't think static in java term where static values persist till the class is loaded into memory).

promote demote