Skip to content

Commit 21833c2

Browse files
committed
Add bypass functionality for Trigger Actions via Type in MetadataTriggerHandler
1 parent a14efd1 commit 21833c2

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

trigger-actions-framework/main/default/classes/MetadataTriggerHandler.cls

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@
7575
* This example will execute all Trigger Actions defined in Custom Metadata for the `Account` sObject.
7676
*
7777
*/
78-
@SuppressWarnings('PMD.CognitiveComplexity')
78+
@SuppressWarnings(
79+
'PMD.CognitiveComplexity, PMD.StdCyclomaticComplexity, PMD.CyclomaticComplexity'
80+
)
7981
public inherited sharing class MetadataTriggerHandler extends TriggerBase implements TriggerAction.BeforeInsert, TriggerAction.AfterInsert, TriggerAction.BeforeUpdate, TriggerAction.AfterUpdate, TriggerAction.BeforeDelete, TriggerAction.AfterDelete, TriggerAction.AfterUndelete {
8082
private static final String DOUBLE_UNDERSCORE = '__';
8183
private static final String EMPTY_STRING = '';
@@ -146,6 +148,36 @@ public inherited sharing class MetadataTriggerHandler extends TriggerBase implem
146148
return MetadataTriggerHandler.bypassedActions.contains(actionName);
147149
}
148150

151+
/**
152+
* @description Bypass the execution of a Trigger Action.
153+
*
154+
* @param actionType The Type of the Trigger Action to bypass.
155+
*/
156+
public static void bypass(System.Type actionType) {
157+
MetadataTriggerHandler.bypassedActions.add(actionType.getName());
158+
}
159+
160+
/**
161+
* @description Clear the bypass for a Trigger Action.
162+
*
163+
* @param actionType The Type of the Trigger Action to clear the bypass for.
164+
*/
165+
public static void clearBypass(System.Type actionType) {
166+
MetadataTriggerHandler.bypassedActions.remove(actionType.getName());
167+
}
168+
169+
/**
170+
* @description Check if a Trigger Action is bypassed.
171+
*
172+
* @param actionType The Type of the Trigger Action to check.
173+
* @return True if the Trigger Action is bypassed, false otherwise.
174+
*/
175+
public static Boolean isBypassed(System.Type actionType) {
176+
return MetadataTriggerHandler.bypassedActions.contains(
177+
actionType.getName()
178+
);
179+
}
180+
149181
/**
150182
* @description Clear all bypasses for Trigger Actions.
151183
*/

trigger-actions-framework/main/default/classes/MetadataTriggerHandlerTest.cls

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,37 @@ private class MetadataTriggerHandlerTest {
631631
);
632632
}
633633

634+
@IsTest
635+
private static void bypassWithTypeShouldSucceed() {
636+
MetadataTriggerHandler.bypass(TestBeforeInsert.class);
637+
638+
System.Assert.isTrue(
639+
MetadataTriggerHandler.bypassedActions.contains(TEST_BEFORE_INSERT),
640+
BYPASSES_SHOULD_BE_CONFIGURED_PROPERLY
641+
);
642+
}
643+
644+
@IsTest
645+
private static void clearBypassWithTypeShouldSucceed() {
646+
MetadataTriggerHandler.bypass(TestBeforeInsert.class);
647+
MetadataTriggerHandler.clearBypass(TestBeforeInsert.class);
648+
649+
System.Assert.isFalse(
650+
MetadataTriggerHandler.bypassedActions.contains(TEST_BEFORE_INSERT),
651+
BYPASSES_SHOULD_BE_CONFIGURED_PROPERLY
652+
);
653+
}
654+
655+
@IsTest
656+
private static void isBypassedWithTypeShouldSucceed() {
657+
Boolean isBypassed;
658+
MetadataTriggerHandler.bypass(TestBeforeInsert.class);
659+
660+
isBypassed = MetadataTriggerHandler.isBypassed(TestBeforeInsert.class);
661+
662+
System.Assert.isTrue(isBypassed, BYPASSES_SHOULD_BE_CONFIGURED_PROPERLY);
663+
}
664+
634665
@IsTest
635666
private static void actionShouldExecuteIfNoRequiredOrBypassPermissionsAreDefinedForSObjectOrAction() {
636667
MetadataTriggerHandler.selector = new FakeSelector(

0 commit comments

Comments
 (0)