Sub topic 15
Avoiding conflicts when several automations share an object
This chapter assumes you know the order of execution at a high level, can read a simple Apex trigger, and have built a record-triggered flow. It covers how to keep several automations on one object from interfering with each other.
Why conflicts happen
An object such as Account often collects automation over the years: a validation rule, a before-save flow, an Apex trigger, and perhaps a leftover Process Builder process. Each tool works correctly alone. Problems appear when they touch the same field, or when one tool's update fires the others again.
Common symptoms:
- A field value changes unexpectedly because two automations write to it.
- A save fails with a "too many SOQL queries" error, because the combined work of every automation counts against one transaction's limits.
- A record update runs the same logic twice, because an update inside an automation re-triggers the object's other automations.
Plan the transaction as a whole
All automations that run in one save share the same governor limits. A flow that runs three queries and a trigger that runs ninety-eight leave no room for anything else. Treat the limits as a budget for the whole object, not for each tool.
The order of execution from the previous chapter gives you the sequence. Before-save flows run before before-triggers, and after-save flows run after after-triggers. Flows with the same trigger type have no guaranteed order unless you set one.
Practical rules
- Use one automation per object and event where you can. Prefer one before-save flow and one Apex trigger per object, instead of many small ones.
- Choose one owner per field. If a flow sets
Rating, no trigger should also set it. Record this in a design note or in the field description. - Set a trigger order for flows. In Flow Builder you can give record-triggered flows an order value. Lower values run first. Use this when a flow depends on another flow's result.
- Use entry conditions. Run a flow only when a relevant field changes, with a condition such as
ISCHANGED({!$Record.AnnualRevenue}). This stops needless runs and reduces loops. - Use before-save logic for same-record changes. It needs no extra DML, so it adds no recursion.
- Keep Apex to one trigger per object. Put the logic in a handler class, so you control the order of the steps yourself.
Guarding against recursion
A trigger that updates records of its own object can fire itself again. A static variable limits this to one run per transaction.
public class AccountTriggerGuard {
public static Boolean hasRun = false;
}
trigger AccountTrigger on Account (after update) {
if (AccountTriggerGuard.hasRun) {
return;
}
AccountTriggerGuard.hasRun = true;
List<Account> toUpdate = new List<Account>();
for (Account acc : Trigger.new) {
if (acc.AnnualRevenue != null && acc.AnnualRevenue >= 1000000
&& acc.Rating != 'Hot') {
toUpdate.add(new Account(Id = acc.Id, Rating = 'Hot'));
}
}
update toUpdate;
}A map of the interactions
Checking for conflicts before you add a tool
Before adding automation to an object, list what already runs there. Check Flow Builder's list of record-triggered flows, the Apex Triggers page, and any remaining Workflow Rules and Process Builder processes. Then test with a bulk load of 200 records, because many conflicts appear only at volume.
Exercise
An Account has a before-save flow that sets Rating, and an Apex after-update trigger that updates the same Account's Rating when revenue changes. Users report that Rating sometimes flips back. Name one fix that gives the field a single owner.
Show answer
Remove the Rating logic from the trigger and keep it in the before-save flow, or the reverse. The before-save flow changes the record in memory without extra DML, so it avoids the second update and the re-run of other automations.