Sub topic 17
Multiple triggers and flows on one object
This chapter assumes you know the order of execution from the earlier chapters and can read a debug log header. It covers what happens when several triggers or flows watch the same object, and how to confirm the real order in your org.
Several triggers on one object
You can create more than one Apex trigger on the same object and event. For example, two before insert triggers on Account are allowed. Salesforce does not guarantee which one runs first. They may run in one order today and another after a deployment.
This causes bugs that are hard to reproduce. Trigger B may read a field that Trigger A was supposed to set. It works in a sandbox and fails in production.
The usual fix is one trigger per object. The trigger holds no logic. It sends each event to a handler class, which runs the steps in an order you choose.
trigger AccountTrigger on Account (before insert, before update, after insert) {
if (Trigger.isBefore) {
System.debug('STEP 1: defaults');
for (Account a : Trigger.new) {
if (a.Description == null) {
a.Description = 'Set in before trigger';
}
}
System.debug('STEP 2: normalize names');
for (Account a : Trigger.new) {
if (a.Name != null) {
a.Name = a.Name.trim();
}
}
} else {
System.debug('STEP 3: after insert work');
}
}In a real project, each step would be a method in a handler class. The principle is the same: the order is written in code, so it can't change.
Several flows on one object
Record-triggered flows have the same issue. Several before-save flows on Account run in the before-save step. Several after-save flows run in the after-save step. Without explicit ordering, you can't rely on the sequence.
Flows give you a tool for this. In Setup, open Flow Trigger Explorer. It lists the flows for an object and event, and lets you set a trigger order number on each flow. Flows with a lower number run first. Flows without a number run after the ordered ones, in no guaranteed order.
Ordering only applies within one step. A before-save flow always runs before before triggers, whatever its number.
Confirming the order with debug logs
Documentation tells you the intended order. A debug log shows what actually happened in your org, including managed packages and automation you didn't write.
- In Setup, open Debug Logs and add a trace flag for your user.
- Set the log level so that Workflow, Validation, Database and Apex Code are at least
INFO. UseFINEfor more detail. - Save a record, then open the new log.
- Search for
CODE_UNIT_STARTED. Each unit is a trigger, flow, or workflow step, and appears in execution order. - Look for
DML_BEGIN,VALIDATION_RULE, andFLOW_START_INTERVIEWS_BEGINlines to place validation and flows between the trigger units. - Search for your own
USER_DEBUGlines, likeSTEP 1above, to see ordering inside a trigger.
A second CODE_UNIT_STARTED for the same trigger and event usually means a workflow field update caused it to fire again.
Exercise
Two before insert triggers on Account both set Description. Which value does the record end up with, and how would you make the result predictable?
Show answer
Salesforce does not guarantee the trigger order, so the final value can vary. Merge both into a single trigger that calls a handler, with the steps in a fixed order. Then check the order in a debug log by searching for CODE_UNIT_STARTED.