Sub topic 12
Avoiding Conflicts Between Automations
This chapter assumes you know the order of execution in broad terms, the shared governor limits, and how to build a record-triggered flow and an Apex trigger. Here you will see what goes wrong when several automations act on the same object and event, and how to prevent it.
Why conflicts happen
Salesforce does not treat Flow and Apex as separate systems. A record save is one transaction, and every record-triggered flow, Apex trigger, validation rule, and similar tool that matches the save runs inside it. Each automation was usually built by a different person at a different time. Each works correctly alone, and the bugs appear only when they run together.
Four kinds of conflict cause most of the trouble:
- Overwriting. Two automations set the same field, and the last one to run wins.
- Recursion. One automation updates a record, which fires another automation, which updates the record again.
- Shared limits. Each automation spends part of the same SOQL, DML, and CPU budget.
- Unclear order. Two automations on the same object and event run in a sequence nobody chose.
Know where each tool sits in the order of execution
Within one save, the platform runs these steps in a fixed sequence. This is a simplified view of the parts that matter here:
- Before-save record-triggered flows run.
- Apex before triggers run.
- System and custom validation, including validation rules, run.
- The record is saved to the database, but not committed.
- Apex after triggers run.
- Workflow rules and other legacy automation run.
- After-save record-triggered flows run.
- Roll-up summaries and other final steps run.
- The transaction commits.
If a step changes the record in a way that needs another save, some earlier steps can run again. An after-save flow that updates the triggering record causes Apex triggers and before-save flows to fire a second time. This is the main source of recursion.
The loop is not infinite, because the platform enforces limits. A runaway loop ends with a "Maximum trigger depth exceeded" error or a limit exception, and the whole transaction rolls back. Your user sees an error and nothing is saved.
Conflict 1: Several automations on the same event
You can have many record-triggered flows and several Apex triggers on one object, all running on "after update". Two facts matter:
- Apex triggers on the same object and event have no guaranteed order.
- Flows can be ordered. Open Flow Trigger Explorer in Setup, or set a trigger order value (1 to 2000) in the flow's properties. The order applies only among flows of the same kind, such as before-save with before-save. A flow cannot be ordered relative to an Apex trigger.
Do not depend on an unspecified order. If automation B needs the result of automation A, put both in one place, or restructure so B reads data that A has already committed. Because Apex triggers cannot be ordered against flows, the most reliable fix is to reduce the number of independent automations.
A common rule is one trigger per object, and a small number of flows per object and event. Within one trigger, you control the order in plain code. In flows, give each flow a single clear purpose and set an explicit trigger order where sequence matters.
Conflict 2: Overwriting the same field
Suppose a before-save flow sets Priority__c to "High" for large deals. An Apex before trigger written years earlier sets Priority__c based on region. Whichever runs last wins, and users see inconsistent values.
Ways to avoid this:
- Give each field one owner. Record in your documentation which automation writes which field.
- Use before-save logic for same-record changes. A before-save flow or before trigger changes the record without an extra DML statement. This also avoids re-firing other automations.
- Compare old and new values. Run logic only when a relevant field changed, so unrelated edits do not trigger writes.
In Flow, use the entry condition "Only when a record is updated to meet the condition requirements". In Apex, compare Trigger.new with Trigger.oldMap.
Conflict 3: Recursion
Recursion happens when automation A's update fires automation B, and B's update fires A. Guard against it in both tools.
In flows, the best guard is a precise entry condition, so the flow starts only when a specific field changes to a specific value. Avoid updating the triggering record in an after-save flow unless you must. If you need to change the same record, use a before-save flow instead.
In Apex, a guard keeps work from repeating inside one transaction. The handler below runs once per record per transaction, and only when StageName changed:
public class OpportunityTriggerHandler {
// Static variables live for the whole transaction.
private static Set<Id> processedIds = new Set<Id>();
public static void afterUpdate(List<Opportunity> newList, Map<Id, Opportunity> oldMap) {
List<Opportunity> changed = new List<Opportunity>();
for (Opportunity opp : newList) {
Opportunity oldOpp = oldMap.get(opp.Id);
if (opp.StageName != oldOpp.StageName && !processedIds.contains(opp.Id)) {
changed.add(opp);
processedIds.add(opp.Id);
}
}
if (changed.isEmpty()) {
return;
}
List<Task> followUps = new List<Task>();
for (Opportunity opp : changed) {
followUps.add(new Task(
WhatId = opp.Id,
Subject = 'Follow up on stage ' + opp.StageName,
ActivityDate = Date.today().addDays(3)
));
}
insert followUps;
}
}trigger OpportunityTrigger on Opportunity (after update) {
OpportunityTriggerHandler.afterUpdate(Trigger.new, Trigger.oldMap);
}The handler uses a Set<Id> and not a single Boolean flag, for a reason related to bulk processing.
A guard by record ID has its own trade-off. If a record legitimately changes stage twice in one transaction, the second change is skipped. For most logic this is acceptable. If it is not, track the pair of record ID and stage value, or redesign so the second change is handled in the first pass.
Conflict 4: Shared limits
Governor limits are counted per transaction, across all automations. If a flow spends 60 SOQL queries and an Apex trigger spends 50, the transaction exceeds the 100-query limit for synchronous work and fails. Neither automation breaks the limit alone. The failure appears only when both run.
Practical habits:
- Keep every automation bulk-safe, with no queries or DML inside loops, as covered earlier in this tutorial.
- Avoid "Get Records" and "Update Records" elements inside flow loops.
- Remove redundant lookups. If three automations each query the same parent Account, consider consolidating them.
- Test with 200 records, not one. A one-record test hides most limit problems.
Conflict 5: Running automation when you do not want it
Data loads, migrations, and integrations often need to bypass automation. If you did not plan for this, people deactivate flows in production, which is risky and easy to forget to undo.
Build a bypass switch from the start. A custom permission works well because you can assign it through a permission set to an integration user. In Apex, check it with FeatureManagement.checkPermission:
trigger OpportunityTrigger on Opportunity (after update) {
if (FeatureManagement.checkPermission('Bypass_Opportunity_Automation')) {
return;
}
OpportunityTriggerHandler.afterUpdate(Trigger.new, Trigger.oldMap);
}In a flow, add the same check to the entry conditions with the $Permission.Bypass_Opportunity_Automation global variable. Create the custom permission in Setup first, and keep its use limited and audited. A bypass that skips validation can hide real data problems.
A conflict-prevention checklist
| Symptom | Likely cause | Fix |
|---|---|---|
| Field value is different from what one automation sets | Two automations write the same field | Assign one owner per field, then move logic into one place |
| "Maximum trigger depth exceeded" | Flow and trigger update the record in a loop | Tighten entry conditions, add an ID-based guard, use before-save |
| Second batch of records is not processed | Static Boolean guard blocks later chunks | Use a `Set<Id>` guard |
| "Too many SOQL queries: 101" only in some saves | Several automations share the same limit | Consolidate queries, bulk-safe all automations, test at 200 records |
| Flow B reads stale data from flow A | No explicit order between flows | Set trigger order in Flow Trigger Explorer, or merge them |
| Data load fails on validation or automation errors | No bypass for integrations | Add a custom permission bypass |
Diagnosing a conflict
When behavior is wrong and you do not know which automation caused it:
- Reproduce the save with a single test record in a sandbox.
- Turn on a debug log for the running user, with Workflow and Apex Code at a detailed level. The log lists each automation in the order it ran.
- Check which flows ran in Setup under Paused and Failed Flow Interviews, and in the debug log's flow entries.
- Look for the same field written twice, or the same object updated repeatedly.
- Use Flow Trigger Explorer to list every flow on the object and event and their order.
- Repeat the save with the bypass permission to confirm which side caused the problem.
Design rules that prevent most problems
- Prefer one trigger per object that delegates to a handler class, and keep flows to one clear purpose each.
- Use before-save automation for changes to the triggering record, and after-save only for work on other records or for actions such as emails and callouts.
- Make entry conditions specific, so logic runs only on the relevant change.
- Choose one tool for each piece of logic on an object when you can. If Flow already handles an object's field updates, avoid adding a trigger that does similar work unless a requirement forces it.
- When you do mix tools, split the responsibilities cleanly. For example, a flow owns the decision logic, and an invocable Apex method owns the complex calculation. This keeps the logic in one visible path.
- Always test at 200 records, with every automation active, in a sandbox that matches production.
Exercise
An after-save flow on Opportunity updates the same Opportunity's Next_Step__c field whenever StageName changes. An Apex after-update trigger on Opportunity also updates the Opportunity whenever any field changes, and users now see "Maximum trigger depth exceeded". Give two changes that remove the loop, and explain why a static Boolean guard in the trigger is a poor fix.
Show answer
First, change the flow to a before-save flow, which sets Next_Step__c on the triggering record without an extra update that re-fires the trigger. Second, make the trigger run only when a relevant field changed, by comparing Trigger.new with Trigger.oldMap, and add a Set<Id> guard of processed record IDs. A static Boolean guard is poor because a transaction with more than 200 records runs the trigger in chunks, and the static variable keeps its value across chunks. After the first chunk sets it to true, later chunks skip their work without any error.
Exercise
Two record-triggered after-save flows on Account both run on update. Flow B needs a field value that Flow A calculates. How can you make the order reliable, and why can't you do the same between Flow A and an Apex trigger?
Show answer
Open Flow Trigger Explorer in Setup, or set the trigger order value in each flow's properties, so Flow A has a lower number than Flow B. Ordering works only among flows of the same kind. Apex triggers cannot be placed in the flow ordering, and multiple Apex triggers on the same object and event have no guaranteed order. For that case, merge the logic into one place, or restructure so that the second automation does not depend on the first.