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
JBabuJBabu 

To prevent trigger from firing if it is called from one class

Hi,

 

I have trigger called "OpportunityChange" this will be fired if there is any change (insert/update/delete) on Oportunity Object.

I have lot of ther clases which does insert/update/delete operations on Opportunity object. I want to stop the trigger "OpportunityChange" from being fired if it is called from a class "OpportunityEquipment" but it needs it be fired if it is called from other classes.

 

Please let me know how to achieve this. (I am ready to change the class "OpportunityEquipment" and "opportunitychange" trigger only to get this functionality but other classes which calls the trigger shouldnot be changed).

 

Thanks,

JBabu.

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Create a class:

 

public class recursionCheck{

Boolean shouldIRun = true;

public static void canIRun(){
     return shouldIRun; 
}

public static void stopRecursion(){
    shouldIrun = false;
}

public static void allowTorun(){
    shouldIRun = true;
}

}

 in your class call recursionCheck.stopRecursion();

 

in your trigger, wrap the contents in and if(recursionCheck.canIRun()){}

 

 

 

 

 

All Answers

VKrishVKrish

If it is a method call you can overload the method and call the overloaded method from the class.

But since it is a trigger, this is what I could think of....

It would be good if you have a data point (say a text field) that specifies some indication that this record is inserted/updated from a class.

When you are inserting/updating a record from the class you can specify desired value to the field.

Then in trigger you can add a if condition to check the data point for specific value and just exit without going further.

Dont forget to change the data point value back to blank so it is not skipped when it is updated from other classes.

Starz26Starz26

Create a class:

 

public class recursionCheck{

Boolean shouldIRun = true;

public static void canIRun(){
     return shouldIRun; 
}

public static void stopRecursion(){
    shouldIrun = false;
}

public static void allowTorun(){
    shouldIRun = true;
}

}

 in your class call recursionCheck.stopRecursion();

 

in your trigger, wrap the contents in and if(recursionCheck.canIRun()){}

 

 

 

 

 

This was selected as the best answer
JBabuJBabu

Thanks a lot Starz and Krish.

Arbind Kumar 12Arbind Kumar 12

@starz26 Thanks for your answer, but the boolean should be static also.

static Boolean shouldIRun = true;