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?
Avoid Validation Errors During Data Loads: When importing bulk data, validation rules might prevent successful uploads.
Enhance Testing Efficiency: Admins and developers can disable specific automations for testing.
Reduce Integration Failures: External systems interacting with Salesforce can bypass unnecessary automation rules to improve performance.
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
Navigate to Setup → Custom Settings.
Click New, name it Bypass_Settings, and select Hierarchy as the setting type.
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!
0 Comments