function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Neil KimNeil Kim 

**Please** public static variable for preventing trigger recursion

Hi, guys.

I've setup static variable for preventing trigger recursion.
Class is below.
public Class checkRecursive{
    private static boolean run = true;
    
    public static boolean runOnce(){
    	if(run){
    		run=false;
    		return true;
    	}
        else
        	return run;
 		
    }

}

This class worked. worked. was.
However, it dosen't work now, I don't know why it stops working.

1. When I use just 1 trigger and use this static function, the 'run' value is already false, so the function returns false at the first time.
    Why?
2. I use this class and the same function for several trigger.
    Is this problem?

Please!!!!
Share your expertise.
Thanks in advance.


 
Tarun J.Tarun J.
Hello,

Can you debug and check if this class/method is already being called from some other trigger which is setting it as False?

-Thanks,
TK
Neil KimNeil Kim
@TK, thanks. As you siad, for one trigger, is one static locking value needed?
 
Igor Androsov 22Igor Androsov 22
You need separate static variables for each trigger or even each operation in trigger such as BEFORE AFTER INSERT UPDATE etc.. Unless you want to stop call to all other triggers based on this single value. Be unusual use case but some time useful technique.
LBKLBK
Hi Young,

You can very well use the same class and method in multiple triggers, unless these triggers execute in the same execution context.

For example, if you have two triggers on the same object that could be executed one after other, this single class may not help you.

Static declaration of a variable (code below) will be executed only once in an execution context
private static boolean run = true;

Scenario 1: If you have two triggers on Account object that are supposed to execute one after other (trg_AfterUpdate1 and trg_AfterUpdate2), and if you are using the following code in both the triggers..
 
if(checkRecursive.runOnce()){
//Your trigger logic here
}
this check will return true on one of the triggers and return false on another.

Scenario 2: If these triggers are on two entirely different objects (like Account and Product2), this method will work without any trouble.

If your triggers are on the same object, I would suggest you to use one set of Boolean variable and method per trigger.

Your class may look like this in Scenario 1.
 
public Class checkRecursive{
    private static boolean runTrigger1 = true;
    private static boolean runTrigger2 = true;
    
    public static boolean runTrigger1Once(){
    	if(runTrigger1){
    		runTrigger1=false;
    		return true;
    	}
        else
        	return runTrigger1;
    }

    public static boolean runTrigger2Once(){
    	if(runTrigger2){
    		runTrigger2=false;
    		return true;
    	}
        else
        	return runTrigger2;
    }
}

Your trigger 1 code will be...
if(checkRecursive.runTrigger1Once()){ 
     //Your trigger logic here 
}
And, trigger2 will be
 
if(checkRecursive.runTrigger1Once()){ 
    //Your trigger logic here 
}

Hope this helps.
 
Tarun J.Tarun J.
Yes, you need separate variables for each trigger in this case.
 
-Thanks,
TK
Lokesh KumarLokesh Kumar
Can you please share your trigger code where you are checking recursive event.

​Thanks 
Neil KimNeil Kim
Thanks, @TK, @LBK, @Igor, @Lokesh,
I understand that static variable is needed for each trigger.

I have one more question.

After I update one record, the update trigger is called.
However, when I checked Tigger.New.size(), it show me that

1. Trigger called twice.
2. First call's Trigger.New.size() is bigger than 1.
    Second call's Trigger.New.size() is 1.

Anybody can explain why this is happened?

Please keep heping me.
Thanks all.
Tarun J.Tarun J.
Check in code if on record update, is it fetching and updating some other records?
LBKLBK
Do a System.debug and log the trigger.new contents.

It will help in identifying the data that gets Inserted / Updated.