You need to sign in to do that
Don't have an account?
NisseKnudsen
Deactivate Trigger via Apex-Controller
Hello Community!
In addition to my previous post, I would like to know now, if there is any way to activate/deactivate triggers within my apex controller. I thought about 'trigger1.deactivate();'
Thanks for your help,
Cheers
Nisse
Hi,
you cannot do this via APEX. You can do such stuff via the meta data API.
Cheers!
Martin
You'd normally bypass a trigger by having a flag on the object. I.e. if you have a trigger on account, you'd have a checkbox on the account called "Bypass Trigger" or similar. Then in the trigger, you'd surround your code with an if statement and only execute if the Bypass Trigger flag is false.
Another alternative is to create a static flag in a class somewhere. For example, put something like this in a class named Statics:
public static Boolean bypassTriggerLogic = false;
Then use this in your trigger:
if ( ! Statics.bypassTriggerLogic) {
// Normal trigger logic
}
And put this in your controller, right before you do whatever DML operation will cause the trigger to fire:
Statics.bypassTriggerLogic = true;
Most of the time, the Boolean will be false, and the bulk of the trigger will run. But when the trigger is called as a result of something in your controller, the Boolean will be true, and the IF statement in the trigger will cause the trigger logic to be bypassed.
Tip: When you write your unit tests, don't forget that once a static variable is given a value, that value persists throughout the whole transaction (that is, the whole unit test), unless you change it. If your controller code sets the static variable, then does a DML operation, you might want to have it re-set the static variable right after the DML operation, just to put it back to its default state.
Can someone please help me what is bypass trigger
Cheers
I think the terminology has caused some confusion. This isn't a "bypass trigger", its a technique to allow trigger code to be turned off based on circumstances.