This template provides a simple starting point for creating Apex triggers in Salesforce. The template includes a trigger and a corresponding handler class. You can use this template to create new triggers for different objects or modify it to fit your specific needs.
To use this template, follow these steps:
-
Clone this repository to your local machine.
-
Modify the
AccountTrigger
example class or create a new class extending theTriggerHandler
class and implement the operations that the trigger will handle. The constructor of your class must call the constructor of theTriggerHandler
superclass. For example:public with sharing class ContactTriggerHandler extends TriggerHandler { private static final WithoutSharingHelper withoutSharingHelper = new WithoutSharingHelper(); public ContactTriggerHandler() { this(null); } public ContactTriggerHandler(String triggerSettings) { super(triggerSettings); } protected override void beforeInsert() { // Insert your BEFORE INSERT logic using the trigger context variables (Trigger.new) and possibly methods of WithoutSharingHelper class for without sharing logic } private without sharing class WithoutSharingHelper { // Insert your without sharing logic } }
-
Modify the
AccountTrigger
or create a new Trigger to specify the object you want to create the trigger for and the event that should trigger the handler. To execute your logic, use therun
method of the handler class. Continuing the example from the previous point:trigger ContactTrigger on Contact (before insert) { new ContactTriggerHandler().run(); }
If you want, you can use the custom metadata
Trigger Settings
to optionally disable the operations of the trigger. Create a newTrigger Settings
record, and to use it, pass itsDeveloperName
to the constructor of the handler. Assuming you have theContact_Trigger_Settings
, the previous example would become:trigger ContactTrigger on Contact (before insert) { new ContactTriggerHandler('Contact_Trigger_Settings').run(); }
-
Deploy your class and trigger to your Salesforce environment using your preferred deployment tool.