Setting test classes to handle disabled triggers

Posted by jack on September 3, 2016


I discovered this as a result of a recent incident updating a client Org. The problem was due to the change set including a SOQL query that selected using an unindexed field – this worked fine in test scripts & sandboxes, but came up with an error in Production owing to the larger (than 20k) records involved in the selection.

Because the test method covering the class/method and trigger, making the trigger inactive in the sandbox and redeploying caused the test method to fail. Initially I wrote a different test method that covered just the class method, and commented out the original test method, but then i realised that if the test could detect whether the trigger was active, I could make the test class so that it works with trigger active or inactive like this:

private class testApp {

   /* tests the appClass and object__c triggers */
   /* this method tests both class/method and trigger together if the trigger is active…… */
   public static testMethod void appClassMethodTriggerTest(){
      ApexTrigger appClassMethodTrigger =
                                [Select Id, Status from ApexTrigger where name=’appClassMethodTrigger’];
      // only run the test if the trigger is active…
      if(appClassMethodTrigger.Status == ‘Inactive’){

 else {

      //<test code for active trigger here >

   //…. other test methods…





Categories: Salesforce

Comments are closed.