Sub topic 19
Multiple triggers and flows on one object
This chapter assumes you know the main steps of the order of execution and can write a basic trigger. It covers a gap in that order: when several pieces of automation sit in the same step, which one runs first?
What the order of execution does and does not decide
The order of execution fixes the sequence between kinds of automation. Before-save flows run before before triggers. Before triggers run before custom validation. After triggers run before after-save flows. This sequence is the same on every save.
The order does not define a sequence among several items of the same kind in the same step. If an object has three before update triggers, they all run in the before trigger step, but Salesforce does not promise which runs first.
| Automation in the same step | Is the order guaranteed? | How you control it |
|---|---|---|
| Several Apex triggers for the same event | No | Use one trigger per object and call handler methods in a fixed order |
| Several record-triggered flows of the same type | Only if you set it | Set a trigger order value in Flow Trigger Explorer |
| Several workflow rules | No | Avoid depending on their order |
| Different kinds (flow, then trigger) | Yes | Fixed by the order of execution |
Several triggers on one object
You can create more than one trigger on the same object and event. Salesforce runs all of them in the same step, but the order can differ between orgs, and it can change after a deployment.
Suppose trigger A sets Rating from AnnualRevenue, and trigger B fills a default AnnualRevenue. If B runs first, A sees the default. If A runs first, A sees an empty value. The code works in a sandbox and fails in production. Nothing in the debug log is wrong. You depended on an order that was never promised.
The usual fix is one trigger per object. The trigger holds no logic. It sends each event to a handler class, where you choose the order by calling methods in sequence.
trigger AccountTrigger on Account (before insert, before update, after insert, after update) {
if (Trigger.isBefore) {
AccountTriggerHandler.beforeSave(Trigger.new, Trigger.oldMap);
} else {
AccountTriggerHandler.afterSave(Trigger.new, Trigger.oldMap);
}
}public with sharing class AccountTriggerHandler {
public static void beforeSave(List<Account> newList, Map<Id, Account> oldMap) {
// Order is explicit: defaults first, then logic that reads them.
setDefaults(newList);
setRating(newList);
}
public static void afterSave(List<Account> newList, Map<Id, Account> oldMap) {
// oldMap is null on insert, so this runs only for new accounts.
if (oldMap == null) {
createFollowUps(newList);
}
}
private static void setDefaults(List<Account> accounts) {
for (Account a : accounts) {
if (a.AnnualRevenue == null) {
a.AnnualRevenue = 0;
}
}
}
private static void setRating(List<Account> accounts) {
for (Account a : accounts) {
a.Rating = (a.AnnualRevenue >= 1000000) ? 'Hot' : 'Cold';
}
}
private static void createFollowUps(List<Account> accounts) {
List<Task> tasks = new List<Task>();
for (Account a : accounts) {
tasks.add(new Task(Subject = 'Follow up', WhatId = a.Id));
}
insert tasks;
}
}The handler keeps the bulk-safe pattern from earlier chapters. It loops over the whole list and uses a single DML statement.
Several flows on one object
Record-triggered flows follow the same idea. You can have many before-save flows and many after-save flows on one object, and each group runs inside its own step:
- All before-save flows run before the before triggers.
- All after-save flows run after the after triggers and after the post-save rules.
Within one group, you can set the order. In Setup, open Flow Trigger Explorer, pick the object and the trigger type, and give each flow a trigger order number. Flows with lower numbers run first. Flows with no number run after the numbered ones, and their order among themselves is not guaranteed.
Flows can also change the data that later automation reads. A before-save flow that sets a field is visible to the before triggers, because the flow ran earlier. A before trigger that changes a field is not visible to the before-save flow. The flow has already finished, and it does not run again for that change.
Interactions between triggers and flows
Flows and triggers share one transaction, so an after-save flow that updates a related record can fire that record's triggers and flows. If an after trigger and an after-save flow both update the same record, the second update starts another round of triggers. The record guard from the recursion chapter protects only the Apex side. Flows have no static variable to share with your trigger.
Practical rules
- Keep one trigger per object, with the logic in a handler class.
- Set trigger order values on flows whenever two flows on the same object touch the same fields.
- Do not let two automations write the same field. If you must, pick one owner for that field and document it.
- Do not mix a trigger and a flow for one business rule unless you have a reason. Two places make the rule hard to follow.
- Confirm the real order in a debug log, as shown in the debugging chapter, rather than trusting what you remember.
Exercise
An org has two before insert triggers on Opportunity. Trigger A sets NextStep based on Amount. Trigger B sets a default Amount when it is empty. In production, NextStep is sometimes wrong for new records. What is the cause, and how do you fix it?
Show answer
Salesforce does not guarantee the order of two triggers for the same event. When A runs before B, it reads an empty Amount. Replace both with one trigger that calls a handler class. In the handler, call the default-setting method first and the NextStep method second, so the order is explicit.