Monday, October 28, 2019

APEX TRIGGER BEST PRACTICES SALESFORCE


Here are the best practices that we should follow while designing triggers.

1) BULKIFY YOUR CODE.

Here is an example of a poorly written code that handles only one record at a time.

trigger singleRecordTriggere on Account(before insert) {
    Account obj = Trigger.New[0]; // Trigger handling a single record
    obj.industry = 'Agriculture';
}

Here is an example of code written to handle bulkification,

trigger contactTrigger on contact (before insert, before update) {
 List<Contact> conList =new List<Contact>();
    for(contact con:trigger.new){
        if(con.firstName="Testing"){
               con.dummyContact__c=true;
                conList.add(con);
            }
   }
}

2) Avoid SOQL Queries or DML statements inside FOR Loops as discussed above.

3) SINGLE TRIGGER PER OBJECT.

If we have multiple triggers on one object we cannot control the order of execution. To avoid multiple triggers on an object, we can make use Trigger, Handler, Helper pattern as shown below.

APEX TRIGGER:

Trigger AccountMainTrigger On Account(Before Insert,After Insert,Before Update,After Update,Before Delete,After Delete,After Undelete){
    AccountMainTriggerHandler obj=new AccountMainTriggerHandler();
    if(Trigger.IsBefore && Trigger.IsInsert){
        obj.handleBeforeInsert();
    }
    if(Trigger.IsAfter && Trigger.IsInsert){
        obj.handleAfterInsert();
    }
    if(Trigger.IsBefore && Trigger.IsUpdate){
        obj.handleBeforeUpdate();
    }
    if(Trigger.IsAfter && Trigger.IsUpdate){
        obj.handleAfterUpdate();
    }
    if(Trigger.IsBefore && Trigger.IsDelete){
        obj.handleBeforeDelete();
    }
    if(Trigger.IsAfter && Trigger.IsDelete){
        obj.handleAfterDelete();
    }
    if(Trigger.IsAfter && Trigger.IsUnDelete){
        obj.handleAfterUnDelete();
    }
}

HANDLER:

public class AccountMainTriggerHandler {
    AccountMainTriggerHelper obj1=new AccountMainTriggerHelper();
    public void handleBeforeInsert(){
        // Call Helper Method1
        obj1.helperMethod1();
    }
    public void handleAfterInsert(){
        // Call Helper Method2
        obj1.helperMethod2();

    }
    public void handleBeforeUpdate(){
        // Call Helper Method3
        obj1.helperMethod3();
    }
    public void handleAfterUpdate(){
        // Call Helper Method4
        obj1.helperMethod4();
    }
    public void handleBeforeDelete(){
        // Call Helper Method5
        obj1.helperMethod5();
    }
    public void handleAfterDelete(){
        // Call Helper Method6
        obj1.helperMethod6();
    }
    public void handleAfterUnDelete(){
        // Call Helper Method7
        obj1.helperMethod7();
    }

}

HELPER:

public class AccountMainTriggerHelper {
    public void helperMethod1(){
        // Logic
    }
    public void helperMethod2(){
        // Logic
    }
    public void helperMethod3(){
        // Logic
    }
    public void helperMethod4(){
        // Logic
    }
    public void helperMethod5(){
        // Logic
    }
    public void helperMethod6(){
        // Logic
    }
    public void helperMethod7(){
        // Logic
    }
}

No comments:

Post a Comment