You need to sign in to do that
Don't have an account?
Neil Kim
**Please** public static variable for preventing trigger recursion
Hi, guys.
I've setup static variable for preventing trigger recursion.
Class is below.
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.
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.
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
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
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..
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.
Your trigger 1 code will be... And, trigger2 will be
Hope this helps.
-Thanks,
TK
Thanks
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.
It will help in identifying the data that gets Inserted / Updated.