- Published on
How to Use Apex Trigger Context Variables
- Authors
- Name
- Tony Geiser
Trigger context variables in Salesforce are special system-provided variables that allow you to access and manipulate data within an Apex Trigger. They provide crucial information about the current operation, such as whether the trigger is running before or after a database operation, which records are involved, and details about the context in which the trigger is executing.
Key Trigger Context Variables
These are the primary context variables available in Apex Triggers:
Trigger.new
:- A list of the new versions of the records that are currently being processed by the trigger.
- Available in
insert
andupdate
triggers. - It’s a
List<SObject>
of the records that are about to be inserted or updated. - Example:
Trigger.new
for anAccount
object will beList<Account>
.
Trigger.old
:- A list of the old versions of the records before they were modified.
- Available in
update
anddelete
triggers. - Useful for comparing the current and previous values.
- Example:
Trigger.old
for anAccount
object will beList<Account>
.
Trigger.newMap
:- A map of the new versions of the records, keyed by their IDs.
- Available in
insert
andupdate
triggers. - Provides efficient access to records by their IDs.
- Example:
Trigger.newMap
for anAccount
object will beMap<Id, Account>
.
Trigger.oldMap
:- A map of the old versions of the records, keyed by their IDs.
- Available in
update
anddelete
triggers. - Useful for quick lookup of the old state of records before changes.
- Example:
Trigger.oldMap
for anAccount
object will beMap<Id, Account>
.
Trigger.isInsert
:- Boolean variable indicating if the trigger was fired due to an
insert
operation.
- Boolean variable indicating if the trigger was fired due to an
Trigger.isUpdate
:- Boolean variable indicating if the trigger was fired due to an
update
operation.
- Boolean variable indicating if the trigger was fired due to an
Trigger.isDelete
:- Boolean variable indicating if the trigger was fired due to a
delete
operation.
- Boolean variable indicating if the trigger was fired due to a
Trigger.isBefore
:- Boolean variable indicating if the trigger was fired before any records were saved to the database.
Trigger.isAfter
:- Boolean variable indicating if the trigger was fired after all records were saved to the database.
Trigger.isUndelete
:- Boolean variable indicating if the trigger was fired after a record was undeleted from the Recycle Bin.
Trigger.size
:- The total number of records in
Trigger.new
orTrigger.old
lists. - Useful for knowing how many records are being processed in bulk operations.
- The total number of records in
How to Use Trigger Context Variables
Let’s look at a few practical examples to understand how these context variables are used within an Apex Trigger.
Trigger.new
and Trigger.old
Example 1: Using This trigger ensures that whenever an Account
is updated, its Industry
field must be different from the previous value.
trigger AccountTrigger on Account (before update) {
for (Account acc : Trigger.new) {
Account oldAcc = Trigger.oldMap.get(acc.Id);
if (acc.Industry != oldAcc.Industry) {
// Custom logic when Industry changes
acc.Description = 'Industry was changed';
}
}
}
Trigger.new
provides the new versions of the records.Trigger.oldMap
allows quick access to the old versions by their IDs for comparison.
Trigger.newMap
and Trigger.isBefore
Example 2: Using This trigger sets a default Status
to "New" for all Account
records before they are inserted, if the Status
is not already set.
trigger AccountTrigger on Account (before insert) {
if (Trigger.isBefore && Trigger.isInsert) {
for (Account acc : Trigger.new) {
if (acc.Status__c == null) {
acc.Status__c = 'New';
}
}
}
}
Trigger.new
is used to iterate over the records being inserted.Trigger.isBefore
andTrigger.isInsert
ensure the logic runs only before an insert operation.
Trigger.isAfter
and Trigger.isInsert
Example 3: Using This trigger creates a default contact for each new Account
after it is inserted.
trigger AccountTrigger on Account (after insert) {
if (Trigger.isAfter && Trigger.isInsert) {
List<Contact> contacts = new List<Contact>();
for (Account acc : Trigger.new) {
Contact con = new Contact(
LastName = 'Default',
AccountId = acc.Id
);
contacts.add(con);
}
insert contacts;
}
}
Trigger.isAfter
andTrigger.isInsert
ensure the logic runs only after new records are inserted.Trigger.new
is used to access the newly inserted records, and their IDs are now available for creating related contacts.
Trigger.isDelete
Example 4: Using This trigger logs a message whenever an Account
is deleted.
trigger AccountTrigger on Account (before delete) {
if (Trigger.isDelete) {
for (Account acc : Trigger.old) {
System.debug('Account being deleted: ' + acc.Name);
}
}
}
Trigger.isDelete
confirms the trigger runs during a delete operation.Trigger.old
contains the records being deleted, allowing you to log or process them before deletion.
Summary of When to Use Each Variable
Trigger.new
: When you need to access or modify the new versions of records being inserted or updated.Trigger.old
: When you need to compare with the old versions of records being updated or deleted.Trigger.newMap
andTrigger.oldMap
: For efficient lookups and operations based on record IDs.Trigger.isInsert
,Trigger.isUpdate
,Trigger.isDelete
,Trigger.isUndelete
: To determine the type of DML operation triggering the event.Trigger.isBefore
andTrigger.isAfter
: To distinguish between pre-save and post-save operations.Trigger.size
: To know the number of records being processed in bulk operations.
By leveraging these context variables appropriately, you can write efficient, maintainable, and effective triggers to handle complex business logic and automate processes within Salesforce.
As always, please refer to the official Salesforce documentation here.