Skip to content

shveeram/salesforce-object-logic-bypass

Repository files navigation

Object Custom Logic Bypass

Context

In most Salesforce projects I’ve worked on, there is always a need to by-pass custom validations or record-triggered flows or triggers to debug a reported issue or to switch off custom logic for data migration.

Features

  • By-pass either at organisation, profile or user level for specific debug situations
  • Individual control switches for custom validations, record-triggered flows and triggers
  • Ease of implementation

Settings

Custom setting configuration called Object Logic Bypass has by default no by-pass for custom validations, record-triggered flows or triggers at the organization level.

ObjectLogicBypassService is an utility method to read the by-pass flags in APEX in a consistent way.

  • bypassTrigger
  • bypassValidation
  • bypassFlow

During data migration or any specific debug scenario, toggles can be switch on to by-pass one or more option for a specific user or profile or even at the organization level. Profile or User level records can be deleted after usage. Note: When changes are made the organization level, make sure to revert to the defaults.

Usage

Custom validations:

When you write custom validations, by-pass is implemented as below:

IF(
  $Setup.Object_Logic_Bypass__c.Custom_Validations__c,
  FALSE,
  OR(
    AND(
      ISPICKVAL(Support_Tier__c, "Silver"),
      Total_Spending__c < $CustomMetadata.Support_Tier__mdt.Silver.Minimum_Spending__c
    ),
    AND(
      ISPICKVAL(Support_Tier__c, "Gold"),
      Total_Spending__c < $CustomMetadata.Support_Tier__mdt.Gold.Minimum_Spending__c
    )
  )
)

Record-Triggered Flows:

In record-triggered flow, ensure that the flow entry condition is configured as Formula Evaluates to True below in addition to any other business logic:

AND(
  $Setup.Object_Logic_Bypass__c.Record_Flows__c = FALSE,
  [Other entry conditions...]
)

Triggers:

In custom triggers, check the by-pass and wrap the trigger handler or logic as shown in the example below:

trigger AccountTrigger on Account (before insert, before update, before delete, after insert, after update, after delete) {
    
    if (!ObjectLogicBypassService.bypassTrigger) {
        system.debug('Trigger not by-passed');
        new AccountTriggerHandler().run();
    }
}

About

Simple by-pass toggle for custom validations or record-triggered flows or triggers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published