6

So I like many of you am using the static variable "trick" to prevent recursion in my triggers.

But I have one object with many triggers on it all of which are using the method outlined here

https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

My question is, is it true that it two triggers fire in the same execution and use the same check, will they share the same static variable values and therefore the execution of the first trigger will prevent the execution of the second one?

3
  • Why not use separate variables for each trigger? Commented Oct 29, 2015 at 23:32
  • That's what I just did, but it's a strange bug my customers are reporting and I haven't been able to recreate it so I wasn't sure if what I was describing was possible? Do you know if static variables will share values across triggers? Commented Oct 29, 2015 at 23:37
  • Of course. If they are in the same execution context Commented Oct 29, 2015 at 23:41

3 Answers 3

2

The problem is that your variables are static. Remove the keyword static and they'll become instance variables and will only apply within the context of each instance of the trigger.

I don't know exactly what pattern you're using, but if all you're doing is calling a single class, use something more like the following:

global class TriggerHelper {

public Boolean isDel= false;
public Boolean isExec = false;
public Boolean isUpdte = false;
public Boolean isInsrt = false;
public Boolean inPrcss = false;
public Boolean isAftr = false;
public boolean isReEntryInsrt = false;
public boolean IsReEntryUpdte = false;

}

Remember that when you do the check if(!TriggerHelper.isReEnterInsrt) all you're doing is testing to see whether or not it's been initialized in your class. You don't need to change it's value. Instead, all you need do is declare a new instance of it by calling TriggerHelper.isReInsert isReInsert = new TriggerHelper.isReEnterInsrt;

3

Those triggers are executing in the same context, so yes, the static variables are shared and the first trigger will prevent the second.

You could either use multiple variables for more fine grained control, or switch to using one of the many trigger frameworks that are knocking about. The Enterprise Patterns are also a good way to go for managing triggers and business logic.

3

Lacey's answer is correct, by applying a trigger framework to you system will resolve your issue and increase the maintainability. However, if you are not so committed to switch to a different design and simply want to resolve your issue, here is the sample code:

public Class checkRecursive{
    private static boolean runATrigger = true;
    private static boolean runBTrigger = true;
    //...
    public static boolean runAOnce(){
        if(runATrigger){
            runATrigger=false;
            return true;
        }else{
            return runATrigger;
        }
    }

    public static boolean resetATrigger() {
        runATrigger = true;
    }
}

So in case you are calling two triggers in a same execution context, and suppose in Object B's trigger you inserted/updated/deleted some A records. Your code should look like the following:

update bList;
checkRecursive.resetATrigger();
update aList;

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.