Contact 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 Contact 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);
}
Contact Trigger
/**
* @description ContactTrigger.
* @Created By: Arun Kumar
* @Created Date : 26-06-2022
* @Last Modified Date : 26-06-2022
*/
trigger ContactTrigger on Contact (before insert,before update, before delete, after insert,
after update, after delete,after undelete) {
TriggerHandler handler = new ContactTriggerHandler(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);
}
}
}
Contact Trigger Handler
public without sharing class ContactTriggerHandler implements TriggerHandler{
private boolean triggerIsExecuting;
private integer triggerSize;
public ContactTriggerHelper helper;
public ContactTriggerHandler(Boolean triggerIsExecuting, integer triggerSize) {
this.triggerIsExecuting = triggerIsExecuting;
this.triggerSize = triggerSize;
this.helper = new ContactTriggerHelper();
}
public void beforeInsert(List<Contact> newContacts){
//call ContactTriggerHandler methods
helper.updateContactAddresses(newContacts);
}
public void beforeUpdate(List<Contact> newContacts, List<Contact> oldContacts, Map<Id,sObject> newContactMap, Map<Id,sObject> oldContactMap){
//call ContactTriggerHandler methods
}
public void beforeDelete(List<Contact> oldContacts, Map<Id,SObject> oldContactMap){
//call ContactTriggerHandler methods
}
public void afterInsert(List<Contact> newContacts, Map<Id,SObject> newContactMap){
//call ContactTriggerHandler methods
}
public void afterUpdate(List<Contact> newContacts, List<Contact> oldContacts, Map<Id,SObject> newContactMap, Map<Id,SObject> oldContactMap){
//call ContactTriggerHandler methods
}
public void afterDelete(List<Contact> oldContacts, Map<Id,SObject> oldContactMap){
//call ContactTriggerHandler methods
}
public void afterUndelete(List<Contact> newContacts, Map<Id,SObject> newContactMap){
//call AccountTriggerHelper methods
}
}
Contact Trigger Helper
public without sharing class ContactTriggerHelper {
//Constructor
public ContactTriggerHelper() {
system.debug('Inside AccounttriggerHelper Constructor');
}
/**
* @description updateContactsCountOnAccount method counts number of contacts on account records.
* @param conList
*/
public void updateContactsCountOnAccount(List<Contact> conList){
//write logic to update the account owner
}
/**
* @description updateAddresses method updates contact address
* copy billing address to contact mailling address
* @param conList
*/
public void updateContactAddresses(List<Contact> conList) {
//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