Sub topic 2
The Seven Trigger Events
This chapter assumes you can write basic Apex (variables, loops, collections), know the Salesforce data model, and have used DML and SOQL. You should also know that governor limits exist.
What a trigger event is
An Apex trigger declares which events it listens to. An event is a combination of a timing (before or after) and a database operation. Salesforce runs the trigger once per event for each batch of records, not once per record.
The declaration lists the events after the object name:
trigger ContactTrigger on Contact (before insert, before update, after insert) {
// Runs only for these three events.
}If you leave an event out, the trigger does not run for it.
The seven events
| Event | Fires | Typical use |
|---|---|---|
| before insert | Before new records are saved | Set defaults, normalize values |
| before update | Before changes are saved | Adjust fields, validate changes |
| before delete | Before records are deleted | Block deletion |
| after insert | After new records are saved | Create related records |
| after update | After changes are saved | Update related records |
| after delete | After records are deleted | Clean up or log related data |
| after undelete | After records are restored from the Recycle Bin | Rebuild related data |
There is no before undelete event. Records in the Recycle Bin cannot be changed before they are restored.
Upsert and merge are not separate events. An upsert fires insert or update events depending on whether each record exists. A merge fires delete events for the losing records and update events for the winner.
Choosing the right event
Pick the event by asking two questions.
- Which operation must the trigger react to? Insert, update, delete, or undelete.
- Does the logic change the records being saved, or does it need data that only exists after the save?
If you only need to change fields on the records being saved, use a before event. If you need the record Id of a new record, for example to create child records, use an after event. These differences are covered in the next chapter.
Here is a trigger that handles one event and does one job:
trigger AccountDeleteGuard on Account (before delete) {
for (Account a : Trigger.old) {
if (a.AnnualRevenue != null && a.AnnualRevenue > 1000000) {
a.addError('High-revenue accounts cannot be deleted.');
}
}
}It uses before delete because the requirement is to stop the deletion. After delete would be too late. Delete events have no Trigger.new, so the trigger reads Trigger.old.
Common mistakes
- Listing every event "just in case". Each extra event adds code paths you must test. Declare only the events you handle.
- Choosing an after event to modify the triggering record. The record is read-only there, so the assignment fails with an error.
- Expecting a trigger to fire on undelete when
after undeleteis not listed. - Forgetting that one trigger body can serve several events. When it does, you must branch on the context, which later chapters cover.
Exercise
Exercise
A requirement says that when an Account is created, a follow-up Task must be linked to it. Which event should the trigger use, and why not a before event?
Show answer
Use after insert. The Task needs the Account's record Id in WhatId, and a new record has no Id until it is saved. In before insert the Id is still null.
Exercise
Which of the seven events has no "before" counterpart, and why?
Show answer
Undelete. A record in the Recycle Bin cannot be edited before it is restored, so Salesforce only offers after undelete.