Sub topic 15
Avoiding conflicts when several automations share an object
This chapter assumes you know the basics of record-triggered flows, Apex triggers, validation rules and the order of execution. It covers what goes wrong when several automations act on the same object, and how to design so they stay predictable.
Why conflicts happen
Most orgs grow automation over years. One admin builds a flow on Account, a developer adds a trigger, and an older Process Builder process is still active. Each tool works alone. Together they can produce problems that no single tool shows.
The common failure modes are:
- Two automations write to the same field, and the last one to run wins.
- One automation's update fires the others again (recursion).
- A combined transaction exceeds a governor limit that each tool stays under on its own.
- A validation rule or required field blocks a save that another automation depended on.
- Logic is duplicated across tools, so a business rule change is made in one place and missed in the other.
Know what runs on the object
Before adding automation, take inventory. For the object, list:
- Validation rules and duplicate rules.
- Before-save and after-save record-triggered flows.
- Apex triggers (look for several triggers on one object).
- Legacy Workflow Rules and Process Builder processes that are still active.
- Approval processes and roll-up summary fields on parent objects.
Setup shows each of these on separate pages, so no single screen lists them all. Keep a short document per object that records each automation, its trigger conditions, and the fields it writes. That document is the most useful conflict-prevention tool you have.
Execution order is only partly controllable
The order of execution fixes the relative position of the tool types. In summary, before-save flows run before before triggers, and after-save flows run after after triggers. Validation rules run after the before-phase logic and before the record is saved.
Within one tool type, you have some control:
- Record-triggered flows: use the Flow Trigger Explorer in Setup to see the flows on an object and set their order. You can also give a flow a trigger order value, which runs lower values first.
- Apex triggers: the order of several triggers on one object is not guaranteed. This is the main reason to have only one trigger per object.
- Legacy tools: Workflow field updates and Process Builder processes run in their own positions in the sequence, so a legacy field update can overwrite a flow's value.
Do not build designs that depend on undocumented ordering between two triggers. Make the order explicit or remove the dependency.
One trigger per object, with a handler
Because multiple triggers have no guaranteed order, use a single trigger per object and delegate to a handler class. The trigger stays thin and the handler holds the logic.
trigger AccountTrigger on Account (before insert, before update, after update) {
if (Trigger.isBefore) {
AccountTriggerHandler.beforeSave(Trigger.new, Trigger.oldMap);
} else if (Trigger.isAfter && Trigger.isUpdate) {
AccountTriggerHandler.afterUpdate(Trigger.new, Trigger.oldMap);
}
}public class AccountTriggerHandler {
public static void beforeSave(List<Account> newList, Map<Id, Account> oldMap) {
for (Account acc : newList) {
if (acc.AnnualRevenue != null && acc.AnnualRevenue >= 1000000) {
acc.Rating = 'Hot';
}
}
}
public static void afterUpdate(List<Account> newList, Map<Id, Account> oldMap) {
List<Id> changedIds = new List<Id>();
for (Account acc : newList) {
Account oldAcc = oldMap.get(acc.Id);
if (acc.AnnualRevenue != oldAcc.AnnualRevenue) {
changedIds.add(acc.Id);
}
}
if (!changedIds.isEmpty()) {
System.enqueueJob(new RecalculateRatingJob(changedIds));
}
}
}The handler compares new and old values so that it acts only on records that actually changed. That check is the main defence against unnecessary re-runs.
Divide responsibilities between tools
A clear split removes most overlap. A workable rule for one object is:
| Responsibility | Preferred tool | Reason |
|---|---|---|
| Changing fields on the saving record | Before-save flow, or before trigger if code is already in use | Needs no extra DML |
| Creating or updating related records | After-save flow | Record ID is available |
| Complex logic or large volumes | Apex trigger with handler | Full control over bulk behavior |
| Slow or callout work | Asynchronous Apex or a flow's asynchronous path | Separate transaction and limits |
| Data quality checks | Validation rules | Run late and block bad saves |
Avoid having a flow and a trigger both set the same field. If they must both touch an object, assign each its own fields and write the boundary into the object document.
Recursion and re-entry
When an after-save automation updates the same record, or a related record that updates the original, the save cycle runs again. Flows and triggers then fire a second time within the same transaction.
Three techniques limit this:
- Entry conditions on flows. Use "Only when a record is updated to meet the condition requirements" so the flow runs when a value changes, not on every save.
- Changed-value checks in Apex. Compare against
Trigger.oldMap, as in the handler above. - Static variable guards. A static Boolean or a Set of processed IDs prevents the same record from being processed twice in one transaction.
public class AccountTriggerGuard {
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;
}
}A plain static Boolean is simpler but has a flaw. A transaction that saves 400 records is split into chunks of 200, and a Boolean set during the first chunk blocks the second. A Set of IDs avoids that problem.
Shared limits
All automations in one transaction share one set of governor limits. A flow that runs three queries and a trigger that runs 98 queries together exceed the limit of 100 synchronous SOQL queries. Each tool looks fine alone.
Keep the combined budget in mind:
- Count queries and DML statements across every tool that fires, not per tool.
- Use bulk-safe patterns: collect IDs, query once, update a list once.
- Move heavy work to an asynchronous path so it gets its own limits.
Retiring and migrating overlap
The most common source of conflict is legacy automation that was never removed. When you migrate a Workflow Rule or Process Builder process to a flow, deactivate the old one in the same release. Leaving both active causes duplicate field updates, duplicate emails, and duplicate tasks.
The following diagram shows a conflict between two writers and how a clear owner resolves it.
Testing the combination
Test each automation alone, then test them together. Apex tests that insert or update records run the flows, validation rules and triggers on that object too, so a test for one automation can fail because another one changed. Treat such failures as evidence of a conflict.
Useful checks:
- Run a bulk test with 200 records, not one.
- Assert the final field values, not only that the code ran.
- Use debug logs to confirm which automations fired and in what order.
- Use a sandbox to test changes that deactivate legacy automation.
A practical checklist
- Inventory every automation on the object.
- Assign each field to a single owner automation.
- Use one trigger per object with a handler class.
- Order record-triggered flows explicitly with Flow Trigger Explorer.
- Add entry conditions and changed-value checks to prevent needless re-runs.
- Add up queries and DML across all tools in the transaction.
- Deactivate legacy automation when its replacement goes live.
- Test the combination in bulk.
Exercise
An Account has a before-save flow that sets Rating to "Hot" for revenue of 1,000,000 or more. A developer adds an Apex before trigger that sets Rating to "Warm" when revenue is under 5,000,000. Accounts with revenue of 2,000,000 now end up "Warm" in some sandboxes and "Hot" in others. Explain the cause and give two ways to fix it.
Show answer
Both automations write Rating, and the final value depends on which runs last. The before-save flow runs before the before trigger in the order of execution, so the trigger normally wins, but other flows, ordering settings or legacy field updates can change the outcome between orgs. Fix it by giving Rating a single owner: either move the whole rating rule into one before-save flow or into the trigger handler, and remove the other. If both must remain, give them non-overlapping conditions so they never write the same field for the same record.
Exercise
A trigger uses a static Boolean named hasRun to avoid recursion. A data load saves 400 Accounts in one transaction. Only the first 200 get processed. Why, and what is the fix?
Show answer
The records are processed in chunks of 200 within the transaction. The Boolean is set to true during the first chunk, so the trigger skips the second chunk. Replace the Boolean with a static Set of record IDs that have been processed, and skip only IDs already in the Set.