Sub topic 5
Keeping the trigger body logic-less
This chapter assumes you know basic Apex syntax, how DML operations work, and that governor limits exist. It covers the logic-less trigger body, which is the first step of the handler pattern. The handler class and the dispatch logic come in later chapters.
What "logic-less" means
A logic-less trigger contains no business rules. It has no loops, no field checks, no SOQL, and no DML. Its only job is to declare which events it listens to and pass control to another class.
The trigger file is hard to unit test and hard to read once it grows. A class is easier to test, can have methods, can be extended, and can be reused. Moving the work into a class keeps the trigger stable while the logic changes.
The trigger body
Here is the complete trigger for Account. It is the only trigger on the object.
trigger AccountTrigger on Account (
before insert, before update, before delete,
after insert, after update, after delete, after undelete
) {
new AccountTriggerHandler().run();
}Three choices in this file matter.
- It subscribes to every event. Adding an event later is then a change to the handler only. You do not have to edit and redeploy the trigger.
- It has one statement. The statement creates a handler and calls
run(). - It has no conditions. The handler decides what to do for each event, including doing nothing.
Subscribing to all seven events costs very little. If the handler ignores an event, the trigger returns almost immediately.
Compare with a trigger that holds logic
This version works, but it mixes concerns in the trigger file.
trigger AccountTrigger on Account (before insert, after insert) {
if (Trigger.isBefore && Trigger.isInsert) {
for (Account acc : Trigger.new) {
if (String.isBlank(acc.Description)) {
acc.Description = 'Created without a description';
}
}
}
if (Trigger.isAfter && Trigger.isInsert) {
List<Task> followUps = new List<Task>();
for (Account acc : Trigger.new) {
followUps.add(new Task(WhatId = acc.Id, Subject = 'Welcome call'));
}
insert followUps;
}
}Problems with this version:
- Each new requirement adds another
ifblock, and the file grows without limit. - You cannot call one block from a test or from another class.
- Adding
before updatemeans editing the event list and the body together. It is easy to forget one of them. - Other developers are tempted to create a second trigger instead of reading this one. That brings back the ordering problem from the earlier chapter.
Rules of thumb
- Name the trigger
ObjectNameTrigger, and name the handlerObjectNameTriggerHandler. Predictable names make both files easy to find. - Keep the trigger to one line of real code. If you want to add a second line, the line probably belongs in the handler.
- Do not put
Trigger.newcasts or checks in the trigger. The handler reads the context variables itself. - Use the same trigger layout for every object. Only the object name and the handler name change.
Checking your understanding
A quick way to review a trigger is to ask whether you could delete the trigger and rewrite it from the handler's name alone. If the answer is yes, the trigger is logic-less. If the trigger holds a rule you would lose, move that rule out.
Exercise
A teammate adds if (Trigger.isInsert) { ... } with a SOQL query to the body of AccountTrigger. Name two reasons this breaks the pattern, and say where the code should go.
Show answer
The trigger is no longer logic-less, so the rule cannot be tested or reused apart from the trigger. It also invites more conditions and logic into the trigger file over time. The query and the check should move into the handler, in the method for the matching operation, such as afterInsert. The trigger should keep only new AccountTriggerHandler().run();.
Exercise
Why does the example trigger list all seven events, even if the handler only handles three of them today?
Show answer
Subscribing to every event means a new requirement for another event only needs a change in the handler. The trigger file stays the same, so there is no extra edit or risk of forgetting to update the event list.