Sub topic 15
Moving Logic into a Handler Class
This chapter assumes you can write a trigger for several events, read Trigger.new and Trigger.oldMap, switch on Trigger.operationType, and keep SOQL and DML out of loops.
Why a trigger file should stay small
A trigger file is hard to test, hard to read once it handles several events, and impossible to call from anywhere else. The usual fix is a handler class. The trigger only passes its context to the class, and the class holds the logic.
This gives you three things:
- The trigger file shows which events it handles and nothing else.
- Each piece of logic is a named method that you can test directly.
- You can keep one trigger per object, which avoids the unpredictable execution order of multiple triggers on the same object.
The trigger
The trigger lists its events and makes one call. It passes the context as arguments instead of letting the handler read Trigger itself.
trigger AccountTrigger on Account (before insert, before update, after insert) {
AccountTriggerHandler.run(Trigger.operationType, Trigger.new, Trigger.oldMap);
}Trigger.oldMap is null on insert. The handler must not read from it in insert branches.
The handler
public with sharing class AccountTriggerHandler {
public static void run(
System.TriggerOperation op,
List<Account> newList,
Map<Id, Account> oldMap
) {
switch on op {
when BEFORE_INSERT, BEFORE_UPDATE {
setDefaults(newList);
}
when AFTER_INSERT {
createFollowUps(newList);
}
when else {
System.debug('Unhandled operation: ' + op);
}
}
}
private static void setDefaults(List<Account> accounts) {
for (Account a : accounts) {
if (String.isBlank(a.Description)) {
a.Description = 'Set by AccountTriggerHandler';
}
}
}
private static void createFollowUps(List<Account> accounts) {
List<Task> tasks = new List<Task>();
for (Account a : accounts) {
tasks.add(new Task(
Subject = 'Follow up with new account',
WhatId = a.Id
));
}
insert tasks;
}
}Each method takes a list and works on the whole list, so the handler stays bulk-safe. setDefaults runs in a before event, so its field changes are saved without DML. createFollowUps runs in an after event, because it needs the new record Ids. It collects the tasks and inserts them once, outside the loop.
Common errors
- Reading
Triggerinside the handler. The handler then fails when a test calls it outside a trigger. Pass the context in as parameters. - Reading
oldMapon insert. It isnull, so callingoldMap.get(...)throws aNullPointerException. - Putting DML in a before-event method. Field changes on
Trigger.newalready save. Extra DML on the same records is unnecessary and can cause recursion. - Forgetting an event. Adding
after updateto the trigger line without awhenbranch sends it towhen else, and nothing happens.
Exercise
A teammate's handler method reads Trigger.new directly instead of taking a list parameter. Give two problems this causes.
Show answer
The method works only when it runs inside a trigger, so a test cannot call it with a list of records it built itself. It also cannot be reused from other code, such as a batch class, because Trigger.new is empty outside a trigger. Passing the records as a parameter fixes both.