Sub topic 1
Anatomy of an Apex Trigger
Prerequisites
This chapter assumes you already know the following:
- Basic Apex syntax: variables, loops, and the List, Set, and Map collections.
- The Salesforce data model: objects, fields, records, and record Ids.
- DML statements and SOQL queries.
- That governor limits exist and cap what one transaction can do.
What a trigger is
An Apex trigger is code that Salesforce runs automatically when a record is inserted, updated, deleted, or undeleted. You don't call a trigger yourself. The platform calls it when a matching data change happens, whether the change came from the UI, an API call, a flow, or other Apex.
A trigger belongs to exactly one object. This chapter looks at its structure. Later chapters cover the events and context variables in detail.
The syntax
Every trigger has three parts: a name, an object, and a list of events.
trigger AccountTrigger on Account (before insert, before update) {
for (Account a : Trigger.new) {
if (String.isBlank(a.Description)) {
a.Description = 'Set by AccountTrigger';
}
}
}trigger AccountTriggerdeclares the trigger and its name.on Accountbinds it to the Account object. For a custom object, use its API name, such asInvoice__c.(before insert, before update)lists the events that fire it. Separate events with commas.- The body in braces holds your logic.
The body can read the Trigger class. Here, Trigger.new is the list of records being saved. The loop gives each record a default description when none exists.
How a trigger runs
The diagram shows where a trigger sits between a save request and the database.
In this chapter's example, the trigger runs before the write. That is why it can change a.Description and have the new value saved without a DML statement. Changing the record again in the same trigger would be redundant.
Rules to remember
- Trigger names must be unique within your org. A common convention is the object name followed by
Trigger. - A trigger fires only for the events in its list. The example above does not run on delete.
- One trigger can handle several events, and an object can have several triggers. The order in which multiple triggers on one object and event run is not guaranteed.
- A trigger works on a batch of records, not a single record. Salesforce passes up to 200 records at a time, so always loop over
Trigger.neworTrigger.old.
Common mistakes
- Using DML on records in a before trigger. Records in
Trigger.neware already part of the save. Edit their fields directly and skipupdate. - Naming an event the object doesn't support. Not every object supports every event, so a save can fail if the trigger is declared incorrectly.
- Assuming a single record. As noted above, triggers receive batches.
Exercises
Exercise
A trigger is declared as trigger InvoiceTrigger on Invoice__c (before insert). Name the object it is bound to and the events that fire it.
Show answer
It is bound to the custom object Invoice__c. It fires only on the before insert event, so updates, deletes, and undeletes do not run it.
Exercise
Why does the AccountTrigger example set a.Description without an update statement?
Show answer
The trigger runs before the record is written to the database. Changes to the records in Trigger.new are saved as part of the original operation, so no DML is needed.