Triggers
Apex can be invoked by using triggers. Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions.
A trigger is Apex code that executes:
- Before or after an insert operation
- Before or after an update operation
- Before or after a delete operation
- Before or after a merge operation
- Before or after an upsert operation
- After an undelete operation
An Apex trigger can also execute after the undelete operation.
For example, you can have a trigger run before an object’s records are inserted into the database, after records have been deleted, or even after a record is restored from the Recycle Bin.
You can define triggers for top-level standard objects that support triggers, such as a Contact or an Account, some standard child objects, such as a CaseComment, and custom objects. To define a trigger, from the object management settings for the object whose triggers you want to access, go to Triggers.
There are two types of triggers.
- Before triggers are used to update or validate record values before they’re saved to the database.
- After triggers are used to access field values that are set by the system (such as a record’s
IdorLastModifiedDatefield), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.
Triggers can also modify other records of the same type as the records that initially fired the trigger. For example, if a trigger fires after an update of contact A, the trigger can also modify contacts B, C, and D. Because triggers can cause other records to change, and because these changes can, in turn, fire more triggers, the Apex runtime engine considers all such operations a single unit of work and sets limits on the number of operations that can be performed to prevent infinite recursion. See Execution Governors and Limits.
Additionally, if you update or delete a record in its before trigger, or delete a record in its after trigger, you will receive a runtime error. This includes both direct and indirect operations. For example, if you update account A, and the before update trigger of account A inserts contact B, and the after insert trigger of contact B queries for account A and updates it using the DML update statement or database method, then you are indirectly updating account A in its before trigger, and you will receive a runtime error.
Before creating triggers, consider the following:
-
upserttriggers fire both before and afterinsertor before and afterupdatetriggers as appropriate. -
mergetriggers fire both before and afterdeletefor the losing records, and both before and afterupdatetriggers for the winning record. See Triggers and Merge Statements. -
Triggers that execute after a record has been undeleted only work with specific objects. See Triggers and Recovered Records.
-
Field history is not recorded until the end of a trigger. If you query field history in a trigger, you don’t see any history for the current transaction.
-
Field history tracking honors the permissions of the current user. If the current user doesn’t have permission to directly edit an object or field, but the user activates a trigger that changes an object or field with history tracking enabled, no history of the change is recorded.
-
Callouts must be made asynchronously from a trigger so that the trigger process isn’t blocked while waiting for the external service’s response. The asynchronous callout is made in a background process, and the response is received when the external service returns it. To make an asynchronous callout, use asynchronous Apex such as a future method. See Invoking Callouts Using Apex for more information.
-
In API version 20.0 and earlier, if a Bulk API request causes a trigger to fire, each chunk of 200 records for the trigger to process is split into chunks of 100 records. In Salesforce API version 21.0 and later, no further splits of API chunks occur. If a Bulk API request causes a trigger to fire multiple times for chunks of 200 records, governor limits are reset between these trigger invocations for the same HTTP request.
-
All triggers define implicit variables that allow developers to access run-time context. These variables are contained in the
System.Triggerclass. -
Trigger code is stored as metadata under the object with which they are associated.
-
Triggers and Order of Execution
When you save a record with an
insert,update, orupsertstatement, Salesforce performs a sequence of events in a certain order. -
Operations That Don’t Invoke Triggers
Some operations don’t invoke triggers.
-
Entity and Field Considerations in Triggers
When you create triggers, consider the behavior of certain entities, fields, and operations.
-
You can write triggers for the FeedItem and FeedComment objects.
-
Trigger Considerations for Knowledge Articles
You can write triggers for KnowledgeArticleVersion objects. Learn when you can use triggers, and which actions don’t fire triggers, like archiving articles.