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
NisseKnudsenNisseKnudsen 

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

HaagenHaagen

Hi,

 

you cannot do this via APEX. You can do such stuff via the meta data API. 

 

Cheers!

 

Martin

bob_buzzardbob_buzzard

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.

MJ09MJ09

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.

sureshsomepallisureshsomepalli

Can someone please help me what is bypass trigger

 

Cheers

bob_buzzardbob_buzzard

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.