[SalesForce] Static variables across multiple triggers in same context

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?

Best Answer

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;

Related Topic