Saturday, September 10, 2022

Trigger Handler Framework Salesforce

 Apex triggers are used to manage records in Salesforce. It enables us to perform operations before or after the event is completed. In the “before event” we generally perform validations to prevent wrong entry into the database. In the “after event” we perform operations such as creating or updating a related record. The trigger is a very powerful mechanism using which we can do operations such as executing SOQL, executing DML operations, calling apex class methods.


A trigger is Apex code that executes before or after the following types of operations:

insert
update
delete
merge
upsert
undelete

New developer generally make mistake of writing multiple trigger on same object and later realize that they cannot control order of execution.

The best practice is to write 1 trigger per object.

Let's now look at how "Trigger Handler Framework" help us in making our job easy.

Trigger Handler Interface:

An interface is a class where we create method signatures but body of the method is not
defined.

If we want to use an interface in another class, the other class need to implement it by providing
the body for all of the methods contained in the interface.


We had already learn about the availability of TRIGGER.NEW, TRIGGER.OLD, TRIGGER.NEWMAP, TRIGGER.OLDMAP with different events  in article shown below
and accordingly we are creating the method signature in below interface.







public interface TriggerHandler {

    void beforeInsert(List<SObject> newRecordsList);

    void beforeUpdate(List<SObject> oldRecordsList, List<SObject> newRecordsList, Map<ID, SObject> oldRecordsMap, Map<ID, SObject> newRecordsMap);

    void beforeDelete(List<SObject> oldRecordsList, Map<ID, SObject> oldRecordsMap);

    void afterDelete(List<SObject> oldRecordsList, Map<ID, SObject> oldRecordsMap);

    void afterInsert(List<SObject> newRecordsList, Map<ID, SObject> newRecordsMap);

    void afterUpdate(List<SObject> oldRecordsList, List<SObject> newRecordsList, Map<ID, SObject> oldRecordsMap, Map<ID, SObject> newRecordsMap);

    void afterUndelete(List<SObject> newRecordsList, Map<ID, SObject> newRecordsMap);

}



The handler class will implement the above interface.


Trigger Handler Class:

public without sharing class OpportunityTriggerHandler implements TriggerHandler {

   OpportunityTriggerHelper helperClass = new OpportunityTriggerHelper();

    public void beforeInsert(List<Opportunity> newOppList) {

        helperClass.methodToHandleBeforeInsert();

    }

    public void beforeUpdate(List<Opportunity> oldOppList, List<Opportunity> newOppList, Map<ID, SObject> oldOppMap, Map<ID, SObject> newOppMap) {

         helperClass.methodToHandleBeforeUpdate();

    }

    public void beforeDelete(List<Opportunity> oldOppList, Map<ID, SObject> oldOppMap) {

        helperClass.methodToHandleBeforeDelete();

    }

    public void afterDelete(List<Opportunity> oldOppList, Map<ID, SObject> oldOppMap) {

        helperClass.methodToHandleAfterDelete();

    }

    public void afterInsert(List<Opportunity> newOppList, Map<ID, SObject> newOppMap) {

        helperClass.methodToHandleAfterInsert();

    }

    public void afterUpdate(List<Opportunity> oldOppList, List<Opportunity> newOppList, Map<ID, SObject> oldOppMap, Map<ID, SObject> newOppMap) {

      helperClass.methodToHandleAfterUpdate();

    }

    public void afterUndelete(List<Opportunity> newOppList, Map<ID, SObject> newOppMap) {

      helperClass.methodToHandleAfterUndelete();

    }

}



Now, Let's define our helper class.

Trigger Helper Class:

public without sharing class OpportunityTriggerHelper {

  

    public void methodToHandleBeforeInsert() {

        System.debug('Inside methodToHandleBeforeInsert');

    }

    public void methodToHandleBeforeUpdate() {

        System.debug('Inside methodToHandleBeforeUpdate');

    }

    public void methodToHandleBeforeDelete() {

        System.debug('Inside methodToHandleBeforeDelete');

    }

    public void methodToHandleAfterDelete() {

        System.debug('Inside methodToHandleAfterDelete');

    }

    public void methodToHandleAfterInsert() {

        System.debug('Inside methodToHandleAfterInsert');

    }

    public void methodToHandleAfterUpdate() {

        System.debug('Inside methodToHandleAfterUpdate');

    }

    public void methodToHandleAfterUndelete() {

        System.debug('Inside methodToHandleAfterUndelete');

    }

   

}


Now, Let's define our trigger on opportunity object.

Note:

operationType:

Returns an enum of type System.TriggerOperation corresponding to the current operation.

Possible values of the System.TriggerOperation enum are: BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE. 



trigger OpportunityTrigger on Opportunity(before insert, before update, before delete, after delete, after insert, after update, after undelete) {

           OpportunityTriggerHandler handler = new OpportunityTriggerHandler();

           switch on Trigger.operationType {

            when BEFORE_INSERT {

                handler.beforeInsert(Trigger.new);

            }

            when BEFORE_UPDATE {

                handler.beforeUpdate(Trigger.old, Trigger.new, Trigger.oldMap, Trigger.newMap);

            }

            when BEFORE_DELETE {

                handler.beforeDelete(Trigger.old, Trigger.oldMap);

            }

            when AFTER_DELETE {

                handler.afterDelete(Trigger.old, Trigger.oldMap);

            }

            when AFTER_INSERT {

                handler.afterInsert(Trigger.new, Trigger.newMap);

            }

            when AFTER_UPDATE {

                handler.afterUpdate(Trigger.old, Trigger.new, Trigger.oldMap, Trigger.newMap);

            }

            when AFTER_UNDELETE {

                handler.afterUndelete(Trigger.new, Trigger.newMap);

            }

        }

}



Let's test our code now.

Open the developer console.

A) Create a opportunity record as shown below.

Trigger Handler Framework Salesforce


Now, Check the logs under developer console.

Trigger Handler Framework Salesforce

Now, let's update the opportunity record.

Trigger Handler Framework Salesforce

No comments:

Post a Comment