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
djaindjain 

Disable Apex Trigger

Hi,
 
In my application i need to disable the trigger while importing data in Contact ,
and to disable the Trigger the sample code is
 
Code:
    private void enableDisableTrigger(String triggerName,Boolean isActive){
     ApexTrigger tr=[Select a.Id from ApexTrigger a where a.Name=:triggerName limit 1];
     if(tr!=null){
      tr.IsValid=isActive;
      update tr;
     }
    }

 
but in this case i get an error: DML not allowed on ApexTrigger
Please guide me.
 
Thanks in advance
 
JimRaeJimRae
Currently, I don't believe there is any way to disable a trigger, except by undeploying it.  This is a frequently requested idea, you should look on the ideas site and vote for your favorites!

You have 2 workarounds that I am aware of.
1) update your trigger to be bulk safe, and allow it to fire on your records as they are imported.  This is probably the best option, if you can figure out how to do it.
2) update your trigger to test for certain bulk conditions, such as the running user being an admin and when the Trigger.new.size() > xxx records. when these are not true, execute the trigger code, otherwise, drop out.