Sub topic 12
Apex triggers: when code is justified
This chapter assumes you know Flow basics, the Salesforce data model, and can read simple Apex or SOQL. Examples use the Account object.
What a trigger is
An Apex trigger is code that runs before or after a record is inserted, updated, deleted, or undeleted. Triggers fire inside the same transaction as the save, so they can change the record or stop the save with an error.
When code is justified
Apply clicks before code first. Choose a trigger only when declarative tools cannot do the job or no longer do it well:
- Logic that needs complex data structures, recursion control, or custom algorithms.
- Very high data volumes, where Flow's per-interview overhead becomes costly.
- Calls to Apex-only features, such as complex callouts or custom sharing logic.
- Behavior your team must unit test and version-control as code.
- Requirements a flow already hits limits on.
If a before-save record-triggered flow can do the same job, prefer the flow. The trigger below is a useful comparison because a flow can replace it.
A bulk-safe before trigger
trigger AccountTrigger on Account (before insert, before update) {
for (Account acc : Trigger.new) {
if (acc.AnnualRevenue != null && acc.AnnualRevenue >= 1000000) {
acc.Rating = 'Hot';
}
}
}Three points make this trigger correct:
Trigger.newholds every record in the batch, so the loop handles 1 record or 200.- It runs in the before context, so changes to
accare saved automatically. Noupdatestatement is needed, and none should be added. - It has no SOQL or DML inside the loop, so it stays within governor limits.
Common errors
| Error | Result | Fix |
|---|---|---|
| SOQL or DML inside a loop | Limit exception on large batches | Collect Ids first, then query or update once |
| Handling only `Trigger.new[0]` | Other records in the batch are skipped | Always loop over the collection |
| Calling `update` on `Trigger.new` in a before trigger | Error or wasted DML | Set fields directly |
| Several triggers on one object | Unpredictable run order | Use one trigger per object |
Choosing the context
- Use before triggers to validate or change the same record.
- Use after triggers when you need the record Id, or when you change related records.
Slow work, such as callouts or large recalculations, belongs in asynchronous Apex, covered in the next chapter.
Exercise
A teammate writes a before update trigger that loops over Trigger.new, sets acc.Rating, and then runs "update Trigger.new;". What is wrong, and what should change?
Show answer
In a before trigger, field changes on Trigger.new are saved automatically, so the update statement is unnecessary and can cause errors or extra DML. Remove the update statement. Also consider whether a before-save record-triggered flow could do this without code.
Exercise
Why must a trigger loop over Trigger.new instead of reading only the first record?
Show answer
A single save can include up to 200 records, such as from a data load or API call. Reading only the first record would skip the rest.