0

I had previously asked how to write test class to cover custom metadata in Test class coverage required for Custom metadata that turns a Trigger on and Off and found solution from the answers.

As a follow up Question, Is calling the doNothing() method in trigger in every method the correct way? or is there a better way to write the code in the triggerHandler for the doNothing() method. This is my trigger code

trigger CaseTrigger on Case (before Insert, before Update,after insert, after update) {
    
         switch on Trigger.operationType{
            when BEFORE_INSERT {
                CaseTriggerHandler.method1(trigger.new);
            }
            when AFTER_INSERT {
                CaseTriggerHandler.method2(trigger.new);
            }
public class CaseTriggerHandler {
     @TestVisible static Boolean bypassTrigger = Trigger_Disabler__mdt.getInstance('Account_Disabler')?.Disable_Trigger__c == true;
    public static void doNothing(){
        if(bypassTrigger == true){
            return;
        }
    }            
    public static void method1(List<Case> newCases> ){
       doNothing();
      //method1 logic
               }
        } 
    }
    public static void method2(List<Case> newCases){
        doNothing();
        //method1 logic
            }
        }
    }

1 Answer 1

2

In my opinion, best practice would be to confirm whether or not you want to do anything within your trigger as soon as possible. That reduces any impact to the UI or other processes occurring for which you want to bypass the trigger.

That means that I would recommend something along the lines of the following:

Existing Trigger

trigger CaseTrigger on Case (before Insert, before Update,after insert, after update) {
    if(!TriggerBypassHandler.checkBypass('Case')){
        switch on Trigger.operationType{
            when BEFORE_INSERT {
                CaseTriggerHandler.method1(trigger.new);
            }
            when AFTER_INSERT {
                CaseTriggerHandler.method2(trigger.new);
            }
    }

New generic trigger bypass check (so that you don't have to re-write this in each trigger handler)

public class TriggerBypassHandler {

    public static Boolean checkBypass(String sObjectName){
        Boolean bypassTrigger = Trigger_Disabler__mdt.getInstance(sObjectName)?.Disable_Trigger__c;
        return bypassTrigger;
    }
}
1
  • Thank you for your response! Mar 22 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .