Trigger Context Variables in Salesforce
Chapter Topics
- Basics of Triggers
- When to use triggers
- Types of Apex Triggers in Salesforce
- Trigger Order of Execution
- Trigger Context Variables in Salesforce
- Trigger.new in Salesforce| Context Variables in Apex Triggers
- Trigger.old in Salesforce | Context Variables in Apex Triggers
- Trigger.newMap in Salesforce| Context Variables in Apex Triggers
- Trigger.oldMap in Salesforce| Context Variables in Apex Triggers
- Trigger Exceptions
- Best Practice in Triggers
- Trigger Helper Class Pattern
Trigger context variables in Salesforce allow developers to access the run-time context of a trigger when it is executed. These variables, available through the System. Trigger class, provide crucial information about the triggering records and the events like before insert, after update, etc.
Trigger Events in Salesforce
Salesforce triggers react to particular record changes, such as insert, update, delete, and undelete. When the trigger is activated by these events, you can do custom logic either prior to or following these DML activities. You can access and modify the records involved in these actions using the context variables, which include Trigger.new, Trigger.old, Trigger.newMap, and Trigger.oldMap.
Context variables and triggers work together to provide Salesforce with strong modifications that facilitate the automation of workflows and the enforcement of business logic.
Types of Trigger Context Variables
Here’s a breakdown of the different types of Trigger Context Variables:
- Trigger.new: A list of the new version of sObject records (available for insert, update, and undelete triggers).
- Trigger.old: A list of the old version of sObject records (available for update and delete triggers).
- Trigger.newMap: A map of record IDs to the new version of sObject records (available for before update, after insert, after update, and after undelete triggers).
- Trigger.oldMap: A map of record IDs to the old version of sObject records (available for update and delete triggers).
Considerations for Trigger Context Variables
Before working with Trigger Context Variables, there are a few key points to consider:
- Modifying Records: You can update the values of fields in Trigger.new, but only in before triggers.
- Read-only Trigger.old: This variable is read-only and cannot be modified.
- Deletion and Upsert Limits: Trigger.new cannot be deleted. Additionally, upsert and merge events do not have unique triggers; they fire based on the DML events related to the merge operation, like insert, update, or delete.
The following table lists considerations about certain actions in different trigger events:
Trigger Event | Can change fields using trigger.new | Can update original object using an update DML operation | Can delete original object using a delete DML operation |
before insert | Allowed. | Not applicable. The original object has not been created; nothing can reference it, so nothing can update it. | Not applicable. The original object has not been created; nothing can reference it, so nothing can update it. |
after insert | Not allowed. A runtime error is thrown, as trigger.new is already saved. | Allowed. | Allowed, but unnecessary. The object is deleted immediately after being inserted. |
before update | Allowed. | Not allowed. A runtime error is thrown. | Not allowed. A runtime error is thrown. |
after update | Not allowed. A runtime error is thrown, as trigger.new is already saved. | Allowed. Even though bad code could cause an infinite recursion doing this incorrectly, the error would be found by the governor limits. | Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible. |
before delete | Not allowed. A runtime error is thrown. trigger.new is not available in before delete triggers. | Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible. | Not allowed. A runtime error is thrown. The deletion is already in progress. |
after delete | Not allowed. A runtime error is thrown. trigger.new is not available in after delete triggers. | Not applicable. The object has already been deleted. | Not applicable. The object has already been deleted. |
after undelete | Not allowed. A runtime error is thrown. | Allowed. | Allowed, but unnecessary. The object is deleted immediately after being inserted. |
Trigger Context Variables Table
Variable | Usage |
isExecuting | Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call. |
isInsert | Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API. |
isUpdate | Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API. |
isDelete | Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API. |
isBefore | Returns true if this trigger was fired before any record was saved. |
isAfter | Returns true if this trigger was fired after all records were saved. |
isUndelete | Returns true if this trigger was fired after a record is recovered from the Recycle Bin. This recovery can occur after an undelete operation from the Salesforce user interface, Apex, or the API. |
new | Returns a list of the new versions of the sObject records.This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers. |
newMap | A map of IDs to the new versions of the sObject records.This map is only available in before update, after insert, after update, and after undelete triggers. |
old | Returns a list of the old versions of the sObject records.This sObject list is only available in update and delete triggers. |
oldMap | A map of IDs to the old versions of the sObject records.This map is only available in update and delete triggers. |
operationType | Returns an enum of type System.TriggerOperation corresponding to the current operation.Possible values of the System.TriggerOperation enum are: BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE. If you vary your programming logic based on different trigger types, consider using the switch statement with different permutations of unique trigger execution enum states. |
size | The total number of records in a trigger invocation, both old and new. |
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.