How do you implement a Trigger Bypass mechanism using Custom Metadata Types or Hierarchy Custom Settings?
Answer
Create a config record with a checkbox such as Bypass_Account_Trigger__c, and check it at the start of the trigger/handler.
- Hierarchy Custom Setting: value can be set per org, profile or user (e.g. bypass for the integration user or during data loads), editable without deployment, and
getInstance()doesn't use SOQL. - Custom Metadata Type: deployable with metadata, good for permanent switches, but not changeable via DML in the same transaction.
- Custom Permission:
FeatureManagement.checkPermission()for per-user bypass.
apex
Trigger_Settings__c s = Trigger_Settings__c.getInstance();
if (s.Bypass_Account_Trigger__c) return;
Also provide a static in-code bypass for tests and for avoiding recursion.
apex-triggertriggerbypasscustom-metadata