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
prabhutumprabhutum 

Ways to bypass a trigger?

Experts,

 

I have a situation where based on where the Update is executed from, I got to decide on whether to execute or bypass the trigger. In conventional programming languages, I would have accomplished this by a context or an instance variable.

 

I am relatively new to Apex and could not get my head around on the best way to achieve this. Any ideas are welcome!

 

Thanks!

Pr@sh...

 

Best Answer chosen by Admin (Salesforce Developers) 
jeffdonthemicjeffdonthemic

The trigger will always fire if it meets the criteria defined by the type of object and operation (Account - before insert). You could prevent the processing of the trigger based upon some value in the collection of records being processed. You might also want to look at perhaps using a static variable (see the Apex docs) or some other custom class.

 

Good Luck

 

Jeff Douglas

Appirio

http://blog.jeffdouglas.com

All Answers

jeffdonthemicjeffdonthemic

The trigger will always fire if it meets the criteria defined by the type of object and operation (Account - before insert). You could prevent the processing of the trigger based upon some value in the collection of records being processed. You might also want to look at perhaps using a static variable (see the Apex docs) or some other custom class.

 

Good Luck

 

Jeff Douglas

Appirio

http://blog.jeffdouglas.com

This was selected as the best answer
kamlesh_chauhankamlesh_chauhan

Hi Prabhutum, This can be achived through some tricky idea. Just take one custome field "CalledFrom" with string type in the objcet on which the trigger is written. You can remove this field from pagelayout so end user can not see that field.

 

Update it with some key value from where that object is being updated and trigger is called.

 

Use this field in the trigger and make a check with this field where it has value 'ABC' or 'XYZ' and you can excute your other part of trigger.

 

Hope you can understand. reply me for any clarification.

 

 

Thanks,

Kamlesh :smileyhappy:

prabhutumprabhutum

Jeff and Kamlesh --  Thank you!

 

I WAS ABLE TO USE YOUR SUGGESTED public static variable AS A SOLUTION.

 

Populating a field with some value was not the cleanest solution for a couple of reasons:

# I knew a scenario from where I wanted to bypass the trigger processing -- there were several unknown scenarios from the record could get updated. Hence, I could set the field with when to bypass but not easily on when to execute.

# If I use the default value of a boolean field as a triggerring condition to execute. That means after bypassing the trigger execution, I have to update the field back to the default with a workflow for any subsequent updates -- which will in turn execute the trigger anyway.

 

Right?