sfdc-trigger-framework icon indicating copy to clipboard operation
sfdc-trigger-framework copied to clipboard

Add a new method ,isTriggerEnabled() for disabling trigger in production based on configuration


Open mainakgupta33 opened this issue 1 year ago • 1 comments
trafficstars

Often we come across scenarios when a bulk update or certain operation(s) are required to be done , while keep the trigger state as inactive. To incorporate that I would like propose to add a new method : protected virtual boolean isTriggerEnabled() to the class. The expectation is that this method is to be overridden by implementing object specific triggerHandler classes so that the developers can put check on configurations (e.g. Custom Metadata / Custom Settings).


Also inside the run method we would require a small change like :

if( !validateRun() && isTriggerEnabled() ) {
      return;
} 

The expectation for the isTriggerEnabled() override is something as below :

public override Boolean isTriggerEnabled() {
        Boolean returnedValue = false;
	/****************************************************************************
        Validate settings from Configuration like Custom metadata / Custom settings
        For each custom object there needs to be a separate configuration record 
       ****************************************************************************/
        Custom_Trigger_Parameter__c trig = Custom_Trigger_Parameter__c.getInstance();
        genesis__Org_Parameters__c orgParam = genesis__Org_Parameters__c.getInstance();
        if(trig?.AppTrigger__c == False && orgParam?.genesis__Disable_Triggers__c == False){
            returnedValue = true;
        }
        return returnedValue;
 }

mainakgupta33 avatar Aug 12 '24 11:08 mainakgupta33

I think Kevin's idea for the framework is for it to be pure Apex, not relying on other metadata to deploy.

Your idea is quite simple to implement without modifying the framework too. All you gotta do is add a check for each trigger event on your handlers and return early if the trigger is disabled.

public void beforeInsert() {
    if (isTriggerEnabled()) {
        // halt trigger execution if the custom metadata check fails
        return;
    }
    
    // continue execution if trigger is enabled
}

You could either copy your snippet onto your trigger handlers or create a virtual class and make your handlers inherit this (which I think is preferred, to save lines of code and extra work).

renatoliveira avatar Aug 12 '24 13:08 renatoliveira