diff --git a/CHANGELOG.md b/CHANGELOG.md index a41f1c8..e1fe90a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 1.1.0 - 2023-11-27 +### Added +- New methods to determine if a specific trigger is enabled and new test methods to cover them. + +### Fixed +- Moved recording of trigger execution to the beginning to address a recursion bug. + +### Changed +- Updated API versions. +- Moved examples to a top-level folder. +- Updated README. + ## 1.0.0 - 2023-09-15 ### Added - Created a new Trigger Framework for Salesforce. \ No newline at end of file diff --git a/examples/classes/AccountTriggerHandler.cls-meta.xml b/examples/classes/AccountTriggerHandler.cls-meta.xml index 7a51829..c14e405 100644 --- a/examples/classes/AccountTriggerHandler.cls-meta.xml +++ b/examples/classes/AccountTriggerHandler.cls-meta.xml @@ -1,5 +1,5 @@ - + - 58.0 + 59.0 Active diff --git a/examples/classes/AccountTriggerHandlerHelper.cls-meta.xml b/examples/classes/AccountTriggerHandlerHelper.cls-meta.xml index 642d054..c14e405 100644 --- a/examples/classes/AccountTriggerHandlerHelper.cls-meta.xml +++ b/examples/classes/AccountTriggerHandlerHelper.cls-meta.xml @@ -1,5 +1,5 @@ - + - 58.0 + 59.0 Active - \ No newline at end of file + diff --git a/examples/triggers/AccountTrigger.trigger-meta.xml b/examples/triggers/AccountTrigger.trigger-meta.xml index 3726578..26b9451 100644 --- a/examples/triggers/AccountTrigger.trigger-meta.xml +++ b/examples/triggers/AccountTrigger.trigger-meta.xml @@ -1,5 +1,5 @@ - + - 58.0 + 59.0 Active diff --git a/force-app/main/default/classes/BaseTriggerHandler.cls b/force-app/main/default/classes/BaseTriggerHandler.cls index b16acb3..32e60f2 100644 --- a/force-app/main/default/classes/BaseTriggerHandler.cls +++ b/force-app/main/default/classes/BaseTriggerHandler.cls @@ -131,6 +131,8 @@ public abstract class BaseTriggerHandler { public void run() { if (this.canRun()) { + this.registerRun(); + switch on this.operationType { when BEFORE_INSERT { this.beforeInsert(); @@ -154,8 +156,6 @@ public abstract class BaseTriggerHandler { this.afterUndelete(); } } - - this.processAfterRun(); } } @@ -176,7 +176,7 @@ public abstract class BaseTriggerHandler { } @TestVisible - private void processAfterRun() { + private void registerRun() { triggerHandlerAndOperation.add(this.triggerHandlerName + this.operationType); } @@ -184,6 +184,14 @@ public abstract class BaseTriggerHandler { // Static methods to disable/enable trigger execution //================================================================================ + public static Boolean triggerIsEnabled(Type triggerHandler) { + return !disabledTriggerHandlers.contains(triggerHandler); + } + + public static Boolean triggerIsEnabled(SObjectType objectType) { + return !disabledTriggerObjects.contains(objectType); + } + public static void disableTriggers(Set triggerHandlers) { if (triggerHandlers != null) { disabledTriggerHandlers.addAll(triggerHandlers); diff --git a/force-app/main/default/classes/BaseTriggerHandler.cls-meta.xml b/force-app/main/default/classes/BaseTriggerHandler.cls-meta.xml index 7a51829..b1a915c 100644 --- a/force-app/main/default/classes/BaseTriggerHandler.cls-meta.xml +++ b/force-app/main/default/classes/BaseTriggerHandler.cls-meta.xml @@ -1,5 +1,5 @@ - 58.0 + 59.0 Active diff --git a/force-app/main/default/classes/BaseTriggerHandlerTest.cls b/force-app/main/default/classes/BaseTriggerHandlerTest.cls index df9b75f..29e29ab 100644 --- a/force-app/main/default/classes/BaseTriggerHandlerTest.cls +++ b/force-app/main/default/classes/BaseTriggerHandlerTest.cls @@ -426,12 +426,12 @@ private with sharing class BaseTriggerHandlerTest { } @IsTest - static void processAfterRun_onFirstRun_valueAddedToSet() { + static void registerRun_onFirstRun_valueAddedToSet() { TestTriggerHandler triggerHandler = new TestTriggerHandler(); triggerHandler.operationType = TriggerOperation.BEFORE_INSERT; Test.startTest(); - triggerHandler.processAfterRun(); + triggerHandler.registerRun(); Test.stopTest(); String triggerHandlerName = triggerHandler.triggerHandlerName; @@ -444,7 +444,7 @@ private with sharing class BaseTriggerHandlerTest { } @IsTest - static void processAfterRun_onSecondRun_valueAddedToSet() { + static void registerRun_onSecondRun_valueAddedToSet() { TestTriggerHandler triggerHandler = new TestTriggerHandler(); triggerHandler.operationType = TriggerOperation.BEFORE_INSERT; @@ -453,7 +453,7 @@ private with sharing class BaseTriggerHandlerTest { BaseTriggerHandler.triggerHandlerAndOperation.add(triggerHandlerName + operationType); Test.startTest(); - triggerHandler.processAfterRun(); + triggerHandler.registerRun(); Test.stopTest(); Assert.isTrue( @@ -464,6 +464,52 @@ private with sharing class BaseTriggerHandlerTest { // Static methods to disable/enable trigger execution + @IsTest + static void triggerIsEnabled_passEnabledType_returnTrue() { + Type triggerHandler = TestTriggerHandler.class; + + Test.startTest(); + Boolean triggerIsEnabled = BaseTriggerHandler.triggerIsEnabled(triggerHandler); + Test.stopTest(); + + Assert.isTrue(triggerIsEnabled, 'Expected the trigger "TestTriggerHandler" to be enabled'); + } + + @IsTest + static void triggerIsEnabled_passDisabledType_returnFalse() { + Type triggerHandler = TestTriggerHandler.class; + BaseTriggerHandler.disabledTriggerHandlers.add(triggerHandler); + + Test.startTest(); + Boolean triggerIsEnabled = BaseTriggerHandler.triggerIsEnabled(triggerHandler); + Test.stopTest(); + + Assert.isFalse(triggerIsEnabled, 'Expected the trigger "TestTriggerHandler" to be disabled'); + } + + @IsTest + static void triggerIsEnabled_passEnabledObject_returnTrue() { + SObjectType objectType = Account.SObjectType; + + Test.startTest(); + Boolean triggerIsEnabled = BaseTriggerHandler.triggerIsEnabled(objectType); + Test.stopTest(); + + Assert.isTrue(triggerIsEnabled, 'Expected the trigger for Account to be enabled'); + } + + @IsTest + static void triggerIsEnabled_passDisabledObject_returnFalse() { + SObjectType objectType = Account.SObjectType; + BaseTriggerHandler.disabledTriggerObjects.add(objectType); + + Test.startTest(); + Boolean triggerIsEnabled = BaseTriggerHandler.triggerIsEnabled(objectType); + Test.stopTest(); + + Assert.isFalse(triggerIsEnabled, 'Expected the trigger for Account to be disabled'); + } + @IsTest static void disableTriggers_passSetOfTypePopulated_disabledTriggerHandlersPopulated() { Set triggerHandlers = new Set{ diff --git a/force-app/main/default/classes/BaseTriggerHandlerTest.cls-meta.xml b/force-app/main/default/classes/BaseTriggerHandlerTest.cls-meta.xml index 7a51829..c14e405 100644 --- a/force-app/main/default/classes/BaseTriggerHandlerTest.cls-meta.xml +++ b/force-app/main/default/classes/BaseTriggerHandlerTest.cls-meta.xml @@ -1,5 +1,5 @@ - + - 58.0 + 59.0 Active diff --git a/force-app/main/default/classes/TriggerUtil.cls-meta.xml b/force-app/main/default/classes/TriggerUtil.cls-meta.xml index 642d054..b1a915c 100644 --- a/force-app/main/default/classes/TriggerUtil.cls-meta.xml +++ b/force-app/main/default/classes/TriggerUtil.cls-meta.xml @@ -1,5 +1,5 @@ - 58.0 + 59.0 Active - \ No newline at end of file + diff --git a/force-app/main/default/classes/TriggerUtilTest.cls-meta.xml b/force-app/main/default/classes/TriggerUtilTest.cls-meta.xml index 642d054..c14e405 100644 --- a/force-app/main/default/classes/TriggerUtilTest.cls-meta.xml +++ b/force-app/main/default/classes/TriggerUtilTest.cls-meta.xml @@ -1,5 +1,5 @@ - + - 58.0 + 59.0 Active - \ No newline at end of file +