I need some suggestions in improving the 'prevent trigger' logic. I have parent 'Lead' object and couple of child objects. I have Trigger Helpers on each of these objects. When these child object records are inserted or updated, I need to update Lead but at the same time, I want to avoid firing the Trigger on Lead(We are doing this to stop firing Lead triggers when we don't need)
Object 1 Trigger:
trigger Object1Trigger on Object1(before update,before insert,after update, after insert) {
if(!Object1TriggerHelper.preventLeadTrigger() && !Object2TriggerHelper.preventLeadTrigger() &&
!Object3TriggerHelper.preventLeadTrigger() && !Object4TriggerHelper.preventLeadTrigger())
{
//Do stuff
}
}
Object 1 Trigger Helper:
public with sharing Object1TriggerHelper{
private static boolean preventLeadTrigger = false;
private static boolean preventObject1Trigger = false;
public static void insert_Object1(List<Object1> recordList)
{
.....
.....
//DO Stuff
preventLeadTrigger = TRUE;
preventObject1Trigger = TRUE;
update object1Records;
update leadRecords;
preventLeadTrigger = FALSE;
preventObject1Trigger = FALSE:
}
Similar code for Object 2,3 & 4
Lead Trigger:
trigger LeadTrigger on Lead(before update,before insert,after update, after insert) {
**//Is there a better approach than this i.e. creating static variable in each TH and then checking here**
if(!Object1TriggerHelper.preventLeadTrigger() && !Object2TriggerHelper.preventLeadTrigger() &&
!Object3TriggerHelper.preventLeadTrigger() && !Object4TriggerHelper.preventLeadTrigger())
{
//Do stuff
}
}
Question: Instead of creating a staic boolean variable in each TH and then checking that in Lead Trigger, is there a better way?
@sfdcfox Thank you for your help. I used the above code and had one issue. When I call the disableTrigger method from my TH class and then check if the trigger is disabled by calling the isTriggerActive method, it returns FALSE which is working fine. But when I call the enableTrigger method and then check the status again, it is still returning FALSE. I made below changes to the enableTrigger & disable methods and it seems to be working fine. Can you please let me know your thoughts i.e my changes are OK or if I have missed something.
public static void enableTrigger(String name) {
if(!current.enabled) {
current.exceptions.add(name);
}else
{
current.enabled = false;
current.exceptions.add(name);
}
}
public static void disableTrigger(String name) {
if(current.enabled) {
current.exceptions.add(name);
}
else
{
current.enabled = true;
current.exceptions.add(name);
}
}