Trigger Framework

It is great to have a good Trigger Framework. In this post, we will see how to build a Trigger Framework and an Opportunity Trigger.

Create a custom settings: It should be used to disable a particular trigger or all of the triggers when it is needed.

Create an Interface: This interface will contain all of the method prototypes. TriggerHandler class should implement this interface.

public interface ITriggerHandler
{
    void beforeInsert(List<SObject> newItems);
    void beforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems);
    void beforeDelete(Map<Id, SObject> oldItems);
    void afterInsert(Map<Id, SObject> mapNewItems);
    void afterUpdate(Map<Id, SObject> mapNewItems, Map<Id, SObject> mapOldItems);
    void afterDelete(Map<Id, SObject> oldItems);
    void afterUndelete(Map<Id, SObject> oldItems);
    // This method needs to be implemented to check whether or not trigger logic should run.
    Boolean isDisabled();
}

Create a Router class: Every trigger should call this class. From here respective Trigger Handler classes should get called

public class TriggerDispatcher
{
    /*
        Call this method from your trigger, passing in an instance of a trigger handler which implements ITriggerHandler.
        This method will fire the appropriate methods on the handler depending on the trigger context.
    */
    public static void run(ITriggerHandler handler)
    {
        // Check to see if the trigger has been disabled. If it has, return
        if (handler.isDisabled())
            return;
        
        // Detect the current trigger context and fire the relevant methods on the trigger handler:

        // Before trigger logic
        if (Trigger.isBefore )
        {
            if (Trigger.isInsert)
                handler.beforeInsert(trigger.new);

            if (Trigger.isUpdate)
                handler.beforeUpdate(trigger.newMap, trigger.oldMap);

            if (Trigger.isDelete)
                handler.beforeDelete(trigger.oldMap);
        }

        // After trigger logic
        if (Trigger.isAfter)
        {
            if (Trigger.isInsert)
                handler.afterInsert(trigger.newMap);

            if (Trigger.isUpdate)
                handler.afterUpdate(trigger.newMap, trigger.oldMap);

            if (Trigger.isDelete)
                handler.afterDelete(trigger.oldMap);

            if (Trigger.isUndelete)
                handler.afterUndelete(trigger.oldMap);
        }
    }
}

Create the handler class.

public class OpportunityTriggerHandler implements ITriggerHandler {
	public static Boolean triggerDisabled = false;
	
	/*
        Checks to see if the trigger has been disabled. For example, you could check a custom setting here.
        In this example, a static property is used to disable the trigger.
        In a unit test, you could use OpportunityTriggerHandler.triggerDisabled = true to completely disable the trigger.
    */
	public Boolean isDisabled(){
		if (triggerDisabled) {
            return true;
        }
		else {
			/*
			Check on custom settings
			*/
			
			return triggerDisabled;
		}
	}
	
	public void beforeInsert(List<SObject> newItems){
		//Call service class to write the logic
	}
	
	public void beforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems){
		//Call service class to write the logic
	}
	
	public void beforeDelete(Map<Id, SObject> oldItems) {
		//Call service class to write the logic
	}
	
	public void afterInsert(Map<Id, SObject> mapNewItems) {
		//Call service class to write the logic
	}
	
	public void afterUpdate(Map<Id, SObject> mapNewItems, Map<Id, SObject> mapOldItems) {
		//Call service class to write the logic
	}
	
	public void afterDelete(Map<Id, SObject> oldItems) {
		//Call service class to write the logic
	}
	
	public void afterUndelete(Map<Id, SObject> oldItems) {
		//Call service class to write the logic
	}
}
public with sharing class OpportunityService {
	/*
	Create methods to write the trigger logic
	If needed create a Helper Class and call helper class-methods
	*/
}
public with sharing class OpportunityTriggerHelper {
	
}
trigger OpportunityTrigger on Opportunity (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
    TriggerDispatcher.run(new OpportunityTriggerHandler());
}