Best Practice in avoiding Trigger Recursion

Handling recursion in apex triggers and the best practices related to it.

Resolution

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(){

if(firstRun){

firstRun = false;

return true;

}else{

return firstRun;

}

}

}

Handle the same in trigger :

============================

trigger updateTrigger on anyObject(after update) {

if(AvoidRecursion.isFirstRun())

{

//Code

}

}

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).