5

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;
    }

}
2
  • Just a quick suggestion. You'd only need a single checkbox on your custom setting (because being able to select both 'active' and 'disabled' at the same time doesn't make any sense).
    – Derek F
    Commented Mar 27, 2017 at 20:45
  • Make sense. You are right.
    – user31302
    Commented Mar 27, 2017 at 20:47

2 Answers 2

9

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 ...

}
3
  • Daniel, I tried the following code and The trigger still runs after being checked and saved. Am I missing something. Any idea.
    – user31302
    Commented Mar 27, 2017 at 23:05
  • @SFDCLearner The trigger will still run, but it should return almost immediately when the first condition indicates the custom setting made it "inactive". Of course, the trigger isn't really inactive. It just isn't doing anything. Commented Mar 27, 2017 at 23:47
  • Do the debug logs show the debug statement appearing? If not, check if you have any profile or user specific hierarchy settings. Commented Mar 27, 2017 at 23:48
0

Create the custom setting and custom fields. Use Manage and give the name as something like Deactivate. then in your trigger code write as,

SwitchTrigger ST= SwitchTrigger.getvalues('Deactivate'); if(ST.Activate==TRUE){ your trigger code }

You must log in to answer this question.