+ Start a Discussion
fer_farfer_far 

Distinguish between cascading triggers or directly fired from operations

Hi,

 

How can I distinguish when a trigger is fired from another trigger (cascading) or directly from a database save operation (not fired from another trigger)?

 

I've been looking for this in Apex Help documents without any success.

 

Thanks in advance.

dave23dave23

Just happened to see this while looking for something else - HTH.

  

Look in the Apex Code Developers under Using Static Methods and Variables. Snippet from that section is:

 

Use static variables to store information that is shared within the confines of the class. All instances of the same class share a single copy of the static variables. For example, all triggers that are spawned by the same request can communicate with each other by viewing and updating static variables in a related class. A recursive trigger might use the value of a class variable to determine when to exit the recursion.

Suppose you had the following class:

public class p { 
   public static boolean firstRun = true; 
}

A trigger that uses this class could then selectively fail the first run of the trigger:

trigger t1 on Account (before delete, after delete, after undelete) { 
       if(Trigger.isBefore){
          if(Trigger.isDelete){
             if(p.firstRun){
                 Trigger.old[0].addError('Before Account Delete Error');
                  p.firstRun=false;
              } 
           }
        }
}