...
Create the handler class by extending the
USYD_TriggerHandlerBase
class.Create include a
constructor
method which carries aString
parameter to pass/contain the trigger name (which can be automatically determined from the trigger body).Override the applicable events (methods) that need to be implemented/executed (i.e.
beforeInsert
,beforeUpdate
, etc.).Write the business logic. It is recommended to have the business logic written in a separate method and invoke it from the overridden event/method, rather than in the event itself. This promotes separation of event invocation from business logic. The developer may also opt to have business logic written in a separate class and just invoke it from the applicable events.
Sample Trigger Handler Class:Code Block language java //AccountTriggerHandler.cls public class AccountTriggerHandler extends USYD_TriggerHandlerBase{ //constructor as explained in step #2 public TriggerTestTriggerHandler(String triggerName) { super(triggerName); } //overridden event as described in step #3 public override void beforeInsert(List<sObject> newRecords){ for (Account record : (Account[])newRecords) { //invoke before insert business logic as described in step #4 } } //overridden event as described in step #3 public override void beforeUpdate(List<sObject> oldRecords, List<sObject> newRecords, Map<ID, sObject> oldRecordMap, Map<ID, sObject> newRecordMap){ for (Account record : (Account[])newRecords) { //invoke before update business logic as described in step #4 } } }
Create the apex Apex trigger.
In the trigger code, determine the name of the current trigger by having the this code just before invoking the trigger handler class:
String triggerName = String.valueOf(this).split(':')[0];
Below the line that determines the trigger name, define and instantiate an instance of the trigger that you have written.
Execute the logic by invoking the trigger handler instance
execute()
method.
Sample Trigger:Code Block language java //AccountTrigger.apxt trigger AccountTrigger on Account(before insert, before update) { String triggerName = String.valueOf(this).split(':')[0]; //step #6 AccountTriggerHandler handler = new AccountTriggerHandler(triggerName); //step #7 handler.execute(); //step #8 }
Create a new
USYD_Trigger_Setting__mdt
custom metadata record with Label/USYD Trigger Setting Name as the name of the Apex trigger.Tick (mark as checked) the trigger events that need to be executed/enabled.