salesforce apex trigger

Salesforce Apex Trigger Handler | The Developer Guide

If we follow best practices, there should be only one trigger per sObject, and in that case, there would be quite a different business logic you would be needing to implement in the Apex Trigger, according to different scenarios. Apex Trigger Handler are classes written to put the business logic code of the trigger and keep the actual trigger free of the business logic. It reduces the complexity of the code and makes it easier to read the logic in the class.

There are different contexts in which you would need to implement the logic in your trigger, and for different requirements, the code would get complexed. Consider the scenario in which you would have to perform different business logic on a single sObject through a trigger, in one which you need to use the newMap and in another in which you would need to use oldMap values.

dont miss out iconDon't forget to check out: Introduction To Collections In Apex | Salesforce Developer Guide

Both might be needed to work on after update condition. In this case, your business logic would be the same, but the values through which the process has to run on, are different. In this case, if you write the logic code inside the trigger only, it would create a huge mess and your code would not be reusable.

To make your code reusable, you can easily create a different class and put your logic code inside a different class. This class is called an Apex Trigger Handler. Just create a different class and call it in the trigger either directly if you have a static method inside the class or by creating an instance of that class if the method is non-static.

For example: Suppose you have a field on Account sObject, and you are required to concatenate all the names of the contacts related to that account on that field, you would have to write an Apex Trigger for that. Now it would work on different contexts (after delete, after insert, after undelete).

Apex Trigger:

trigger ContactNameOnAccount on Contact (after insert, after update, after delete, after undelete) {
    if(trigger.isAfter){
        if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
            ContactNameOnAccountClass.updateContactOnAccount(trigger.new);
        }
        if(trigger.isDelete || trigger.isUpdate){
            ContactNameOnAccountClass.updateContactOnAccount(trigger.old);
        }
    }
}

dont miss out iconCheck out another amazing blog by Krati here: Explore the Benefits of Salesforce for Small Businesses

Apex Trigger Handler

public class ContactNameOnAccountClass {
    public static void updateContactOnAccount(List<Contact> listOfContacts){
        //receiving list of Contact from trigger as parameter
        Set<Id> accountSet = new Set<Id>();
        List<Account> listOfAccountToBeUpdate = new List<Account>();
        // Add each contact's AccountId in a set
        for(Contact con : listOfContacts) {
            accountSet.add(con.AccountId);
        }        
        //Query and inner query to get id and all contact from account and name from child contact        
        List<Account> listOfAccountWithContact = [SELECT Id, All_Contacts__c,
                                                     (SELECT Id, Name FROM Contacts ORDER BY LastModifiedDate DESC) 
                                                     FROM Account Where id IN :accountSet];
        for(Account acc : listOfAccountWithContact){
            Account newAccount = new Account();
            String strAllContacts = '';
            for(Contact con :acc.Contacts){
                //iterating each contact to add their name.
                strAllContacts= strAllContacts + con.Name + ' , '; 
            }
            newAccount.All_Contacts__c = strAllContacts.removeEnd(' , ');
            newAccount.Id = acc.id;
            listOfAccountToBeUpdate.add(newAccount);//adding to the list
        }   
        if(!listOfAccountToBeUpdate.isEmpty()) {
            update listOfAccountToBeUpdate; 
        }
    }  
}

Popular Salesforce Blogs