Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mainakgupta33 opened this issue Aug 12, 2024 · 1 comment

Comments

@mainakgupta33
Copy link

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

@renatoliveira
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants