SAVE UP to ₹4999/- OFF On All Salesforce Courses Claim Offer

9

Trigger Context Variables in Salesforce

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:

  1. Trigger.new: A list of the new version of sObject records (available for insert, update, and undelete triggers).
  2. Trigger.old: A list of the old version of sObject records (available for update and delete triggers).
  3. 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).
  4. 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 EventCan change fields using trigger.newCan update original object using an update DML operationCan delete original object using a delete DML operation
before insertAllowed.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 insertNot 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 updateAllowed.Not allowed. A runtime error is thrown.Not allowed. A runtime error is thrown.
after updateNot 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 deleteNot 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 deleteNot 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 undeleteNot allowed. A runtime error is thrown.Allowed.Allowed, but unnecessary. The object is deleted immediately after being inserted.

Trigger Context Variables Table

VariableUsage
isExecutingReturns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsertReturns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdateReturns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDeleteReturns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBeforeReturns true if this trigger was fired before any record was saved.
isAfterReturns true if this trigger was fired after all records were saved.
isUndeleteReturns 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.
newReturns 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.
newMapA 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.
oldReturns a list of the old versions of the sObject records.This sObject list is only available in update and delete triggers.
oldMapA map of IDs to the old versions of the sObject records.This map is only available in update and delete triggers.
operationTypeReturns 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.
sizeThe total number of records in a trigger invocation, both old and new.
Salesforce certifications
Next Topic

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.

Book A 15-Minutes Free Career Counselling Today!