Q:

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 

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

A:

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

A:

There are couple of design concepts/patterns published to handle the apex triggers and some of them had gotten wide acceptance. Check out Dan Appleman's advanced apex programming or Tony Scott's design pattern. You might want to check out my architecture framework to handle the apex triggers as well. This is the most comprehensive architecture framework to handle the apex triggers in Force.com platform that supports recursive triggers along with SOC and others. While none of these design patterns/frameworks can be universal solution, depending on your requirements, I believe they should be hugely beneficial. 
Edit: 
To answer your question:
a. 'Trigger.isExecuting' can be used anywhere in the apex code. It returns true, if you call it within the trigger context (not just within the trigger, for e.g. if you have a separate class and if you call the method in this class from your trigger, it will still return true).
b. This is best utilized in your helper code, specifically, if the helper code could be called from different places. For e.g. the same helper method in your helper class could be called from your custom controller or trigger code. So call 'Trigger.isExecuting' will help you to identify if the call is from trigger or a visualforce page.
c. At the simplest level, you need a static variable to control the recursion and this is separate from using 'Trigger.isExecuting'. You can either get rid of TriggerContextUtility class, but you still need to have a static variable somewhere and set/unset it to control the recursion. 
Here is an example of how to use the static variable and Trigger.isExecuting. This code handles the after update event on the Account object. Note that this example code may not follow the best practices and supposed to demonstrate the concept.
trigger AccountTrigger on Account (after insert, after update) {
    AccountTriggerHandler.handleAccountEvents(Trigger.isExecuting, Trigger.isBefore, Trigger.isAfter, Trigger, isInsert, Trigger.isUpdate, Trigger.new, Trigger.old);
}

public class AccountTriggerHandler {
    // you need one static variable for each event type. 
    public static Boolean isAfterInsertFlag = false;
    public static Boolean isAfterUpdateFlag = false;

    public static void handleAccountEvents(Boolean isExecuting, Boolean isBefore, Boolean isAfter, Boolean isInsert, Boolean isUpdate, List<Account> listNewAccounts, List<Account> listOldAccounts) {

        if(isAfter && isUpdate) { // handle after update event
            if(isAfterUpdateFlag == false) { // if this is the first call
                isAfterUpdateFlag = true; // set the static variable to indicate that the call is in progress for after update event.
                AccountHelper.updateOwner(isExecuting, listNewAccounts);
            }
        }
    }
}

public class AccountHelper {
    public static void updateOwner(Boolean isTrigger, List<Account> listAccounts) {

        if(isTrigger) { // if the running context originated from trigger invocation
            // do what ever you want to do as part of the trigger invocation
        } else {
            // do what ever you want to do if the call originated from different context, such as from controller.
        }
    }
}

