Learn from industry experts! Admin batch starts from 19 Feb.
Hurry up!
Book your slot now!

9

Trigger Helper Class Pattern

What is the Trigger Handler Pattern in Salesforce?

The Trigger Handler Pattern in Salesforce is a design pattern used to organize and manage the logic executed within Apex triggers. This pattern promotes cleaner code and better maintainability by using trigger logic in a dedicated handler class rather than directly in the trigger itself. This functionality makes it a best practice for apex triggers.

Common Avoidable Practice:

Salesforce record changes → Trigger containing all the performing code, executes → End

Best Practice:

Salesforce record changes → Trigger calls out to one or multiple classes → Class contains the performing code which executes → End

Trigger Helper Class Apex

A Trigger Helper Class is an Apex class that contains methods to handle various operations for triggers. This class includes methods for different trigger events (e.g., `before insert`, `after update`) and processes the logic relevant to each event.

According to “Best Practices” suggested by Salesforce, we should always use a Helper Class (Apex Class) with a Trigger. It makes it easy to maintain the code in the long term.

Trigger Helper Class Examples

Here is an example of Trigger Helper Class:

trigger AccountTrigger on Account (before insert, before update) {
    AccountTriggerHandler.handleTrigger(Trigger.new, Trigger.oldMap,
Trigger.operationType);
}

Trigger Handler Class

A Trigger Handler Class is a dedicated Apex class that encapsulates the logic associated with a specific Salesforce trigger. It typically contains methods that correspond to various trigger events (such as before insert, after update, etc.). The primary purpose of this class is to separate the business logic from the trigger itself, promoting cleaner code and better maintainability.

public class AccountTriggerHandler {
    public static void handleTrigger(List<Account> newAccounts, Map<Id, Account>
oldAccountMap, Schema.OperationType operationType) {
        if (operationType == Schema.OperationType.BEFORE_INSERT) {
            handleBeforeInsert(newAccounts);
        } else if (operationType == Schema.OperationType.BEFORE_UPDATE) {
            handleBeforeUpdate(newAccounts, oldAccountMap);
        }
    }

    private static void handleBeforeInsert(List<Account> accounts) {
        for (Account acc : accounts) {
            // Example logic: Set a default value
            if (acc.Industry == null) {
                acc.Industry = ‘Other’;
            }
        }
    }
    private static void handleBeforeUpdate(List<Account> accounts, Map<Id, Account> oldAccountMap) {
        for (Account acc : accounts) {
            // Example logic: Check if the name has changed
            if (oldAccountMap.get(acc.Id).Name != acc.Name) {
                acc.Description = ‘Name changed.’;
            }
        }
    }
}

Advantages of Trigger Handler Pattern

Below listed are some advantages of using the Trigger Handler Pattern as a best practice:

  1. Separation of Concerns: To facilitate simpler reading and maintenance, business logic is maintained apart from trigger code.
  2. Reusability: Logic contained in a handler can be applied to other classes or triggers.
  3. Easier Testing: Unit tests are easier to develop and more targeted when handler classes are tested apart from triggers.
  4. Decreased Complexity: Triggers can easily grow complex. The trigger code is kept simple and clear by using a handler class.
  5. Centralized Logic: It is simpler to manage modifications when all business logic pertaining to a particular object is gathered in one location.
  6. Scalability: Adding new methods or changing existing ones in a dedicated class is simpler as business requirements expand.
  7. Improved Organization: Coders work together more effectively and more efficiently when they follow a consistent structure.
salesforce-developer
Next Chapter

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book A 15-Minutes Free Career Counselling Today!