[SalesForce] Activate and Deactivate trigger using Custom Setting

I have an Apex class called ShippingAddressTriggerHandler and Trigger called ShippingAddress on object Shipping. I want to create a custom setting called SwitchTrigger with checkbox Activate.

Checking Active field on custom setting should Activate the Trigger and not checking should deactivate it.

Can someone suggest how can I achieve this use case

My Trigger:

    trigger ShippingAddress on Shipping_Address__c (after delete, 
                             after insert,  
                             after undelete, 
                             after update, 
                             before delete, 
                             before insert, 
                             before update ) {
    try  
    {
        ITriggerHandler handler = new SingleTriggerHandler (
                                        new ShippingAddressTriggerHandler(),
                                        Trigger.isExecuting,
                                        Trigger.isInsert, 
                                        Trigger.isUpdate, 
                                        Trigger.isDelete, 
                                        Trigger.isBefore, 
                                        Trigger.isAfter, 
                                        Trigger.isUndelete, 
                                        Trigger.new, 
                                        Trigger.newMap, 
                                        Trigger.old, 
                                        Trigger.oldMap, 
                                        Trigger.size );
        handler.process(); 
    }
    catch (AbstractTriggerHandler.InvalidTriggerInvocationException itie)
    {
        SystemExceptionLogger.insertSystemExceptionLogEntry(
                                        itie, 
                                        'Shipping Address Application - Common Use', 
                                        'Shipping Address Trigger', 
                                        SystemExceptionLogger.EXCEPTION_LEVEL_INFO);

    }
    catch (Exception e)
    {
        SystemExceptionLogger.insertSystemExceptionLogEntry(
                                        e, 
                                        'Shipping Address Trigger Application - Common Use', 
                                        'Shipping Address Trigger', 
                                        SystemExceptionLogger.EXCEPTION_LEVEL_CRITICAL);
        throw e;
    }

}

Best Answer

One Checkbox Custom Setting field should be sufficient to toggle between active and inactive states for the trigger.

First thing in your trigger, check the hierarchy custom setting value and return if it is deactivated. Hierarchy custom settings are ideal here as you can turn them on/off for the whole org, a profile, or an individual user. This can be really useful when bulk loading data.

trigger ShippingAddress on Shipping_Address__c (after delete, ...) {

     if(!SomeCustomSetting__c.getInstance().ActivateTheTrigger__c) {
         System.debug('Bypassing trigger due to custom setting');
         return;
     }

     // Rest of the trigger body ...

}
Related Topic