[SalesForce] Trigger .isExecuting Clarification

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

  1. What trigger.isExecuting is actually telling us
  2. How to best leverage it
  3. If it can help me get rid of the TriggerContextUtility class that I currently use to avoid recursive trigger calls.

Thanks a lot

Best Answer

Excellent questions.

"What trigger.isExecuting is actually telling us"

When decoupling the Apex trigger handler class from the actual trigger, the Apex class has no way to know what context it's called in (unit test, web service, visualforce page, trigger). This flag just means the handler was executed by a trigger.

A better name for this flag might be "isCreatedByTrigger".

"How to best leverage it"

At one point I think the intent was to use this flag in unit testing, but Test.isRunningTest() (introduced in v20?) is probably a better solution today.

"If it can help me get rid of the TriggerContextUtility class that I currently use to avoid recursive trigger calls."

All solutions to this problem require some form of static boolean state management, so your code is correct. The isExecuting flag is not applicable to this class of problem.

In fact, the next iteration of my trigger template will probably need to incorporate a static variable to address the recursion problem. Thanks for pointing this out!

-Mike Leach