Bypass Logic Implementation in Salesforce: A Framework for Skipping Automations


 Salesforce environments often contain multiple automations, including validation rules, workflows, process builders, and triggers. However, in certain scenarios, such as data migrations, integrations, or admin testing, you may need to bypass these automations selectively.

This blog will guide you through implementing a Bypass Logic Framework using Custom Settings to control when automations should be skipped, ensuring flexibility while maintaining system integrity.


Why Implement Bypass Logic?

  1. Avoid Validation Errors During Data Loads: When importing bulk data, validation rules might prevent successful uploads.

  2. Enhance Testing Efficiency: Admins and developers can disable specific automations for testing.

  3. Reduce Integration Failures: External systems interacting with Salesforce can bypass unnecessary automation rules to improve performance.

  4. Improve Flexibility: Users can control which rules should run without modifying code frequently.


Solution Approach

We will use Hierarchy Custom Settings to create a bypass configuration that allows us to:

  • Skip all validations or specific ones.

  • Easily control bypassing without modifying metadata.

  • Avoid unnecessary trigger executions.

Step 1: Create a Custom Setting

  1. Navigate to Setup → Custom Settings.

  2. Click New, name it Bypass_Settings, and select Hierarchy as the setting type.

  3. Create the following custom fields:

    • Bypass_Triggers__c (Checkbox)

    • Bypass_Validations__c (Checkbox)

    • Bypass_Flows__c (Checkbox)

    • Specific_Automation_Bypass__c (Text)

Step 2: Implement Bypass in a Validation Rule

Let’s assume we have a validation rule that enforces the AccountNumber field on the Account object.

Validation Rule Without Bypass Logic

ISBLANK(AccountNumber)


This rule will prevent an Account from being saved without a AccountNumber.

Enhanced Validation Rule with Bypass Logic
1
2
3
AND(NOT($Setup.Bypass_Settings__c.Bypass_Validations__c), OR( ISBLANK($Setup.Bypass_Settings__c.Specific_Automation_Bypass__c),
NOT(CONTAINS($Setup.Bypass_Settings__c.Specific_Automation_Bypass__c, 'Account_Number_is_Required'))),
ISBLANK(AccountNumber))


Extending the Bypass Framework to Triggers

To apply the same logic to Apex Triggers, we can check the custom setting inside the trigger before executing logic:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
trigger AccountTrigger on Account (before insert, before update) {
    if (BypassUtils.isGlobalBypassEnabled('Bypass_Triggers__c') || 
        BypassUtils.isSpecificAutomationBypassed('AccountTrigger')) {
            System.debug('Bypassed');
        return; // Exit if bypass is enabled
    }

    // Your trigger logic here
    AccountTriggerHandler handler = new AccountTriggerHandler();
    if(handler.isFirstTime)
    {
        handler.isFirstTime = false;
        switch on Trigger.operationType {
            when BEFORE_INSERT {
                handler.beforeInsert(Trigger.New);
            }
            when BEFORE_UPDATE {
                handler.beforeUpdate(Trigger.New,Trigger.Old, Trigger.newMap, Trigger.oldMap);
            }
        }
    }
}

Utility Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class BypassUtils {
    public static Boolean isGlobalBypassEnabled(String bypassField) {
        try {
            Bypass_Settings__c setting = Bypass_Settings__c.getInstance(); // Get org-wide defaults
            if (setting == null) {
                return false;
            }
            return (Boolean) setting.get(bypassField);
        } catch (Exception e) {
            System.debug('Error in isGlobalBypassEnabled: ' + e.getMessage());
            return false;
        }
    }

    public static Boolean isSpecificAutomationBypassed(String automationName) {
        try {
            Bypass_Settings__c setting = Bypass_Settings__c.getInstance(); // Get org-wide defaults
            if (setting == null || String.isEmpty(setting.Specific_Automation_Bypass__c)) {
                return false;
            }
            return setting.Specific_Automation_Bypass__c.contains(automationName.trim());
        } catch (Exception e) {
            System.debug('Error in isSpecificAutomationBypassed: ' + e.getMessage());
            return false;
        }
    }
}


Conclusion

Implementing a Bypass Logic Framework using Custom Settings ensures flexibility in managing Salesforce automations while preventing unnecessary validation failures. This approach provides an easy way to selectively enable or disable rules without modifying code frequently.

This method enhances your Salesforce architecture by making automations adaptable for various business needs, such as testing, integrations, and data migrations.

Would you like to extend this logic to Flows, Process Builders, or Workflows? Let me know in the comments below!

Feel free to download the complete code from Github - https://github.com/batra-kapil/Bypass-Logic-Basic

Checkout Complete Video Tutorial Below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments