Account Trigger Template

Use only one trigger per SObject Type

Multiple triggers on the same object are not recommended because Salesforce cannot guarantee the order of execution, so we should only add one trigger per object.

So, let’s look at the Account Trigger Template and see how we can use one trigger per object.

Trigger Handler Interface

An interface is similar to a class in which none of the methods have been implemented—the method signatures are present, but the method bodies are empty. To use an interface, another class must implement it by providing a body for all of the interface’s methods.

Interfaces can help your code by adding a layer of abstraction. They separate the specific implementation of a method from its declaration. This way, depending on your application, you can have different implementations of a method.

There should be only one TriggerHandler Interface in the entire org, not one for each object.

public interface TriggerHandler {
// ********** before methods *******************
void beforeInsert(List<SObject> newRecords);
void beforeUpdate(List<SObject> newRecords, List<SObject> oldRecords,
Map<Id,SObject> newRecordMap,Map<Id,SObject> oldRecordMap);
void beforeDelete(List<SObject> oldRecords, Map<Id,SObject> oldRecordMap);
// *********** after methods ********************
void afterInsert(List<SObject> newRecords, Map<Id,SObject> newRecordMap);
void afterUpdate(List<SObject> newRecords, List<SObject> oldRecords,
Map<Id,SObject> newRecordMap, Map<Id,SObject> oldRecordMap);
void afterDelete(List<SObject> oldRecords, Map<Id, SObject> oldRecordMap);
void afterUndelete(List<SObject> newRecords, Map<Id,SObject> newRecordMap);
}
Account Trigger
/**
* @description AccountTrigger.
* @Created By: Arun Kumar
* @Last Modified Date : 18-06-2022
*/
trigger AccountTrigger on Account (before insert,before update, before delete,
after insert, after update, after delete,after undelete) {
TriggerHandler handler = new AccountTriggerHandler(Trigger.isExecuting, Trigger.size);
switch on Trigger.operationType {
when BEFORE_INSERT {
//call before insert handler method
handler.beforeInsert(Trigger.new);
}
when BEFORE_UPDATE {
//call before update handler method
handler.beforeUpdate(Trigger.new, Trigger.old, Trigger.newMap, Trigger.oldMap);
}
when BEFORE_DELETE{
//call before delete handler method
handler.beforeDelete(Trigger.old, Trigger.oldMap);
}
when AFTER_INSERT{
//call after insert handler method
handler.afterInsert(Trigger.new,Trigger.newMap);
}
when AFTER_UPDATE{
//call after update handler method
handler.afterUpdate(Trigger.new, Trigger.old, Trigger.newMap, Trigger.oldMap);
}
when AFTER_DELETE{
//call after delete handler method
handler.afterDelete(trigger.old, Trigger.oldMap);
}
when AFTER_UNDELETE{
//call after undelete handler method
handler.afterUndelete(trigger.new, Trigger.newMap);
}
}
}
Account Trigger Handler
public without sharing class AccountTriggerHandler implements TriggerHandler{
private boolean triggerIsExecuting;
private integer triggerSize;
public AccountTriggerHelper helper;
public AccountTriggerHandler(Boolean triggerIsExecuting, integer triggerSize) {
this.triggerIsExecuting = triggerIsExecuting;
this.triggerSize = triggerSize;
this.helper = new AccountTriggerHelper();
}
public void beforeInsert(List<Account> newAccounts){
//call AccountTriggerHelper methods
}
public void beforeUpdate(List<Account> newAccounts, List<Account> oldAccopunts, Map<Id,sObject> newAccountMap, Map<Id,sObject> oldAccountMap){
//call AccountTriggerHelper methods
}
public void beforeDelete(List<Account> oldAccounts, Map<Id,SObject> oldAccountMap){
//call AccountTriggerHelper methods
}
public void afterInsert(List<Account> newAccounts, Map<Id,SObject> newAccountMap){
//call AccountTriggerHelper methods
}
public void afterUpdate(List<Account> newAccounts, List<Account> oldAccounts, Map<Id,SObject> newAccountMap, Map<Id,SObject> oldAccountMap){
//call AccountTriggerHelper methods
}
public void afterDelete(List<Account> oldAccounts, Map<Id,SObject> oldAccountMap){
//call AccountTriggerHelper methods
}
public void afterUndelete(List<Account> newAccounts, Map<Id,SObject> newAccountMap){
//call AccountTriggerHelper methods
}
}
Account Trigger Helper
public without sharing class AccountTriggerHelper {
//Constructor
public AccountTriggerHelper() {
system.debug('Inside AccounttriggerHelper Constructor');
}
/**
* @description updateAccountOwner method updates accounts owner records.
* @param accList
*/
public void updateAccountOwner(List<Account> accList){
//write logic to update the account owner
}
/**
* @description updateAddresses method updates contact address if account address is changed,
* if more than record found with same zipcode then it gets random record.
* @param accList
*/
public void updateContactAddress(List<Account> accList) {
//write update contacts logic below
}
//Write other methods here as per your requirements
/******************** ADD MORE METHODS*************************/
}

Subscribe to our blog to receive notifications when new content is available.

Join 751 other followers