Apex trigger
An Apex trigger is Apex code that Salesforce runs automatically before or after records of a given object are inserted, updated, deleted or undeleted. Before triggers validate or change records before they are saved; after triggers work with values set by the system and act on other records.
Syntax and events
A trigger is declared as trigger Name on Object (events) { … }. The available events are before insert, before update, before delete, after insert, after update, after delete and after undelete, and one trigger can handle several of them.
Before triggers are for updating or validating values before the save. After triggers are for reading system-set fields such as the record Id or LastModifiedDate and changing other records; the records that fire an after trigger are read-only.
Trigger context
The Trigger class exposes the records and the circumstances of the call: Trigger.new and Trigger.newMap for the new versions, Trigger.old and Trigger.oldMap for the previous ones on update and delete, Boolean flags such as isBefore and isInsert, operationType, and size. Records in Trigger.new can only be modified in before triggers.
Bulk processing and limits
A trigger does not receive one record at a time. DML operations with more than 200 records are processed in batches of 200 and the trigger is invoked once per batch. Because Apex enforces per-transaction governor limits, a query or DML statement inside a loop over the records can exceed them; Salesforce's best practice is to collect records and Ids into sets and maps, query once with an IN clause, and perform DML on collections.
Callouts must be made asynchronously from a trigger, and when several triggers exist on the same object for the same event, Salesforce does not guarantee the order in which they run.
How aprity helps
aprity reads each trigger from the metadata read-only and places it in the per-object execution graph, grouped by save event alongside flows and validation rules. An impact analysis on an Apex class, a field or an object computes deterministically what depends on it, and the integration map is built from the Apex call-sites found in the code.
Related terms
- Salesforce order of execution — The fixed sequence in which Salesforce runs flows, triggers, validation rules and other automation when a record is saved.
- Record-triggered flow — A Flow that Salesforce launches automatically when a record of a given object is created, updated or deleted.
- Before-save vs after-save flow — The two optimisations of a record-triggered flow: Fast Field Updates run before the save, Actions and Related Records run after it.
- Salesforce Dependency API — The Tooling API object MetadataComponentDependency, which returns which metadata components in an org reference which others.
Sources
Official Salesforce documentation this entry was checked against. Platform behaviour changes between releases; the linked page is the reference.