Sub topic 1
What an Apex Trigger Is and When It Runs
A trigger is Apex code that Salesforce runs automatically before or after a record is inserted, updated, deleted, or undeleted. This chapter covers the anatomy of a trigger, the events it responds to, and where it sits in a transaction. Later chapters build on this to cover bulkification.
Prerequisites
This chapter assumes you know:
- Basic Apex syntax, including classes, methods, and control flow.
- The Salesforce data model and what an sObject is.
- Basic SOQL and DML statements.
- That triggers fire when records are inserted, updated, deleted, or undeleted.
Anatomy of a trigger
A trigger is declared with the trigger keyword, a name, the sObject it watches, and a list of events in parentheses.
trigger AccountTrigger on Account (before insert, before update) {
for (Account a : Trigger.new) {
if (a.Name != null) {
a.Name = a.Name.trim();
}
}
}This trigger runs on Account records. It fires before an insert and before an update. It trims whitespace from each name.
The event list uses these values:
| Event | Fires |
|---|---|
| `before insert` | Before new records are saved |
| `before update` | Before changed records are saved |
| `before delete` | Before records are deleted |
| `after insert` | After new records are saved |
| `after update` | After changed records are saved |
| `after delete` | After records are deleted |
| `after undelete` | After records are restored from the Recycle Bin |
There is no before undelete. A trigger can list any combination of these events.
When a trigger runs
A trigger runs inside the save sequence of a transaction. A transaction is one unit of work that either fully commits or fully rolls back. Any of these actions can start a trigger:
- A DML statement in Apex.
- A change made in the Salesforce user interface.
- An API call, such as a data load.
- A flow or other automation that saves records.
The trigger does not care about the source. It sees the records and the event.
The order matters for what you can do. A before trigger runs before the record is written to the database, so you can change field values on Trigger.new without a DML statement. An after trigger runs once the record has an Id. You use it to work on related records.
The real sequence has more steps, such as validation rules, duplicate rules, and flows. The diagram shows only the parts that matter here. If a trigger throws an unhandled exception, the whole transaction rolls back.
Records arrive as a list
A trigger does not receive one record. It receives a batch, and the batch can hold up to 200 records. The variable Trigger.new is a list of the new versions of those records. This is why the loop in the example above is not optional. A trigger written as if only one record exists will break on the first data load.
Every trigger in the same transaction also shares one set of governor limits. These are caps that Salesforce enforces on resources such as SOQL queries. For example, a synchronous transaction allows 100 SOQL queries. The next chapters explain how those limits interact with batches of 200.
Practical rules
- Keep one trigger per object. Salesforce does not guarantee the order of multiple triggers on the same object and event.
- Write the trigger to handle a list from the start.
- Use
beforefor changes to the records being saved. Useafterfor work on other records.
Exercise
A trigger needs to set a default value on a field of the record being inserted. Should it use a before insert or an after insert event, and why?
Show answer
Use before insert. The trigger can change the field on Trigger.new directly, and Salesforce saves the change with the record. No extra DML statement is needed.
Exercise
Why must a trigger loop over Trigger.new instead of reading only the first record?
Show answer
Salesforce can pass up to 200 records in one trigger invocation, such as during a data load. Reading only the first record would skip the rest.