Sub topic 12
Avoiding Conflicts Between Automations
This chapter assumes you know the basics of Flow and Apex, governor limits, and the order of execution in a single transaction. Here you will see what goes wrong when several automations run on the same object and event, and how to prevent it.
Why conflicts happen
A record save runs inside one transaction. Before-save flows, before triggers, validation rules, after triggers, after-save flows, and other automations all run in that transaction. They share the same governor limits and can read and write the same fields. Salesforce runs several record-triggered flows on the same object and event, but it does not guarantee the order unless you set one. Apex triggers have no guaranteed order relative to each other.
The most common problems are:
- Overwritten values. Two automations set the same field. The last one to run wins, and the result can change after a deployment.
- Recursion. An automation updates a record, which fires the same automation again.
- Shared limits. Each automation looks cheap alone. Together they exceed the 100 SOQL query limit or the 150 DML statement limit.
- Hidden dependencies. One automation relies on a field value that another one sets, with no documented order.
Pick one owner per object and event
The simplest defense is a rule: for each object and event (for example, Contact after insert), one automation owns the logic. Other automations call into it or stay out.
In practice, that means:
- List every automation on the object: triggers, flows, validation rules, and any managed-package logic.
- Group them by event: before save, after save, and delete.
- Merge the logic that touches the same fields into one flow or one trigger.
- Record the decision in the object's documentation or a team wiki.
If the logic needs both tools, keep the entry point in one place. For example, use one record-triggered flow that calls an invocable method, as in the earlier chapter. Do not run a flow and a trigger side by side to do related work.
Control the order when you cannot merge
Flows on the same object and event can be given a trigger order in Flow Trigger Explorer. Lower numbers run first. Use this when you must keep separate flows, such as ones owned by different teams. Apex triggers have no equivalent setting, which is a strong reason to keep one trigger per object and delegate to handler classes.
Guard against recursion
A trigger that updates the same object it fires on can call itself. A common guard is a static variable, which holds its value for the length of the transaction.
public with sharing class AccountRecursionGuard {
private static Set<Id> processedIds = new Set<Id>();
public static Boolean shouldProcess(Id recordId) {
if (processedIds.contains(recordId)) {
return false;
}
processedIds.add(recordId);
return true;
}
}Tracking record IDs works better than a single Boolean. A Boolean would block records in a later batch of the same transaction that still need processing. In flows, reduce recursion by adding entry conditions, such as "only when Status is changed", and by using before-save flows to set fields on the triggering record instead of running a second update.
Check for changes before acting
Many conflicts disappear when each automation runs only when it needs to. In Apex, compare Trigger.oldMap with the new values:
trigger OpportunityTrigger on Opportunity (before update) {
for (Opportunity opp : Trigger.new) {
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
if (opp.StageName != oldOpp.StageName) {
// Run stage-change logic only for records that changed stage
}
}
}In a flow, use the entry condition "Condition Requirements" with "Only when a record is updated to meet the condition requirements". This avoids repeated runs.
Test the combination
Tests that cover one automation do not prove the combination is safe. Insert and update records in bulk, such as 200 at once, with all automations active. Assert the final field values, not only that no error occurred. Run this test again whenever someone adds or changes an automation on the object.
Exercise
A Contact has a record-triggered flow that sets Account.Rating and an Apex trigger that also sets Account.Rating. Users report the rating flips between values. What two changes would you make?
Show answer
First, assign one owner for the Account.Rating logic, by merging it into either the flow or the trigger (or having the flow call an invocable method). Second, remove the duplicate write and add entry conditions or a changed-value check so the remaining automation runs only when relevant. If separate flows must stay, set their order in Flow Trigger Explorer.