Sub topic 14
Working Alongside Apex Triggers and Other Flows
This chapter assumes you know the order of execution, the difference between before-save and after-save flows, and how recursion arises. The question here is what happens when several automations act on the same object.
Where each automation runs
Flows and Apex triggers share one transaction and one set of governor limits. Their relative order is fixed by the platform, as this simplified sequence shows.
Three consequences follow.
- A before Apex trigger sees the values a before-save flow has already set.
- An after-save flow sees changes made by after triggers. It also sees the values that before-save flows and before triggers wrote.
- A before-save flow cannot see the changes an after-save flow makes later in the same transaction.
Ordering several flows on one object
Several record-triggered flows can run on the same object and trigger event. By default, the platform does not guarantee the order in which they run. If one flow depends on another, set a trigger order.
- Open the flow's start element and choose Advanced, or use Flow Trigger Explorer in Setup.
- Give the flow a Trigger Order value from 1 to 2000.
- Lower numbers run first. Flows without a value run after those that have one, in no guaranteed order.
Ordering applies separately to before-save and after-save flows for the same object and event. You cannot place a before-save flow after an after-save flow. Flow Trigger Explorer shows both groups for an object, so use it to review what runs on a save.
Prefer to remove the dependency when you can. Two flows that each update the same field are a design problem, and an ordering number only hides it.
Apex triggers and flows together
Apex triggers cannot be ordered against flows beyond the fixed sequence above. Apex triggers on the same object and event have no guaranteed order among themselves either. Keep these practices in mind.
- Use one automation tool per object where possible. If a team already uses a trigger framework, add logic there instead of adding a flow that does the same work.
- Count shared limits. A flow's SOQL queries and DML statements count against the same 100 queries and 150 DML statements as Apex.
- Plan for re-entry. An after-save flow that updates the triggering record makes a second save. Before and after Apex triggers and flows then run again.
Apex static variables survive across that second save, because it happens in the same transaction. The trigger below uses one to skip repeated work.
trigger OpportunityTrigger on Opportunity (after update) {
if (OpportunityTriggerGuard.hasRun) {
return;
}
OpportunityTriggerGuard.hasRun = true;
// Work that must happen once per transaction goes here.
}public class OpportunityTriggerGuard {
public static Boolean hasRun = false;
}This guard has a weakness. In a bulk load of more than 200 records, the platform calls the trigger once per batch of 200 within a single transaction. The flag then skips every batch after the first. Track processed record Ids in a static Set<Id> for bulk-safe code.
A flow cannot read Apex static variables. Guard flows with entry conditions instead, such as ISCHANGED and a "Only when a record is updated to meet the condition requirements" run setting.
Common errors
| Symptom | Likely cause |
|---|---|
| Field value differs from what the flow set | An Apex trigger or later flow overwrote it |
| Flow runs twice on one save | An after-save update on the same record caused re-entry |
| Intermittent limit errors | Flow and Apex queries or DML add up in one transaction |
| Order changes between deployments | No Trigger Order value is set |
Exercise
A before-save flow sets Opportunity.NextStep, and a before-update Apex trigger reads NextStep. Which one sees the other's change, and how can you make the dependency explicit?
Show answer
The Apex trigger sees the flow's value, because before-save flows run before before Apex triggers. Nothing needs ordering between them. If two before-save flows depended on each other, you would set their Trigger Order values and check them in Flow Trigger Explorer.