So I have been wanting some clear cut answers on this for a while and most of the documentation that I can find is fairly vague.
Mike Leach has an excellent post explaining the best practices in creating triggers. I follow this construct and have 1 and only 1 trigger on each object, and have a handler for each object where I can call different methods to execute the logic for the triggers.
http://www.embracingthecloud.com/2010/07/08/ASimpleTriggerTemplateForSalesforce.aspx
This all works great. My question is how to use trigger.isExecuting() effectively.
Right now in order to avoid recursive calls in my triggers I have created a simple class
public class TriggerContextUtility {
private static boolean firstRun = true;
public static boolean isFirstRun() {
return firstRun;
}
public static void setFirstRunFalse(){
firstRun = false;
}
}
That I can call within my trigger to check if the trigger has already been called within the context. I use it like this.
trigger OpportunityTrigger on Opportunity (after insert, after update, before insert, before update) {
OpportunityTriggerHelper helper = new OpportunityTriggerHelper();
if(trigger.isAfter && trigger.isUpdate){
if(TriggerContextUtility.isFirstRun()){
helper.helperFunction(trigger.new, trigger.old, trigger.newMap, trigger.oldMap);
TriggerContextUtility.setFirstRunFalse();
}
}
}
This works fine, I just want to make sure I am doing this in the most efficient way. I am just wondering if I can use the trigger.isExecuting context variable to accomplish the same thing. I can't really find this context variable well explained anywhere. The basic definition I find is
"Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call."
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
I just dont understand fully what that really means and therefore can't figure out how to leverage it. I see that Mike Leach uses includes the variable in his code, but never actually checks it or has any logic based on it.
Can someone help to explain
- What trigger.isExecuting is actually telling us
- How to best leverage it
- If it can help me get rid of the TriggerContextUtility class that I currently use to avoid recursive trigger calls.
Thanks a lot