Sub topic 18
Where to put your logic
This chapter assumes you can write a basic Apex trigger, know validation rules and Flow, and understand DML (insert, update, delete). It turns the order of execution into practical placement rules.
Start with the step you need
Choose the step by what the logic must do. The rest follows from the order of execution.
| You need to... | Put it in | Why |
|---|---|---|
| Set or adjust fields on the record being saved | Before-save flow or before trigger | Changes apply directly. No extra DML, and no second save. |
| Reject a save based on the final values | Validation rule | Runs after before triggers, so it sees their changes. |
| Use the new record's Id | After trigger or after-save flow | The Id does not exist in before insert. |
| Create or update related records | After trigger or after-save flow | The record is saved, though not yet committed. |
| Call an external system | Asynchronous Apex, such as a queueable | Callouts are not allowed in the middle of the transaction. |
Prefer the simplest tool
Use a before-save flow for simple field updates. It is fast and needs no code. Use a trigger when you need complex logic, collections, or tight control over ordering. Keep one trigger per object and move the logic into a handler class, so the order within the object is easy to read.
A placement example
This trigger does both kinds of work, each in the correct step. The before branch sets a field with no DML. The after branch uses the Id and writes related records in one bulk statement.
trigger AccountPlacement on Account (before insert, after insert) {
if (Trigger.isBefore) {
for (Account a : Trigger.new) {
if (a.Description == null) {
a.Description = 'Set in before trigger';
}
}
} else {
List<Task> tasks = new List<Task>();
for (Account a : Trigger.new) {
tasks.add(new Task(Subject = 'Follow up', WhatId = a.Id));
}
insert tasks;
}
}Common placement errors
- Using an after trigger to change the same record.
Trigger.newis read-only in after triggers, so you would need another update. That update fires the triggers again. - Updating the same object from an after trigger without a guard. A static set of processed Ids stops repeat work when a workflow field update re-fires the trigger.
- Putting queries or DML inside a loop. Governor limits are shared across the whole transaction, including flows and other triggers. Bulk the work.
- Depending on the order of several automations. Where possible, consolidate them, because the order of multiple triggers on one object is not guaranteed.
Decision flow
Exercise
A requirement says to set a default Region on every new Account and also create a Contact linked to it. Where should each part go, and why?
Show answer
Set the Region in a before insert trigger or before-save flow, because it changes the saving record with no DML. Create the Contact in an after insert trigger or after-save flow, because it needs the Account Id, which exists only after the record is saved. Insert the Contacts in one bulk DML statement.