Curriculum
Trigger Context Variables are predefined Salesforce variables that provide information about the current trigger execution. They help developers understand what event is occurring, access records involved in the transaction, compare old and new values, and implement business logic based on trigger context.
Trigger Context Variables are one of the most important concepts in Apex Trigger development because they allow developers to build intelligent, scalable, and event-driven automation solutions.
Whether creating validation logic, updating related records, tracking field changes, or processing bulk data, Trigger Context Variables provide the information required to make decisions during trigger execution.
Trigger Context Variables are system-provided variables available inside Apex Triggers.
They provide information such as:
Salesforce automatically populates these variables during trigger execution.
Without Context Variables:
Trigger Cannot Determine
• Event Type
• Record Data
• Previous Values
• Current Values
Context Variables provide the necessary information for automation.
Work with records being processed.
Detect field changes.
Execute different actions based on events.
Reduce unnecessary processing.
Handle multiple records effectively.
These benefits make Context Variables essential.
Salesforce provides several Trigger Context Variables.
Each variable serves a specific purpose.
Trigger.new contains the new version of records being inserted, updated, or undeleted.
Data Type:
List<SObject>
Available In:
for(Student__c student :
Trigger.new){
System.debug(
student.Name
);
}
The trigger processes new record values.
Rahul Sharma
Priya Singh
All new records can be accessed.
Trigger.old contains previous versions of records.
Data Type:
List<SObject>
Available In:
for(Student__c student :
Trigger.old){
System.debug(
student.Status__c
);
}
Previous values are available for comparison.
Pending
Active
Useful for change tracking.
Trigger.newMap stores new records in a Map.
Data Type:
Map<Id,SObject>
Available In:
Maps provide faster record lookup.
Student__c student =
Trigger.newMap.get(
recordId
);
Records can be accessed directly by ID.
Maps are preferred for large datasets.
Trigger.oldMap stores previous record values in a Map.
Data Type:
Map<Id,SObject>
Available In:
Useful for comparing old and new values.
Student__c oldStudent =
Trigger.oldMap.get(
recordId
);
Previous data can be retrieved efficiently.
Business Requirement:
Track status changes.
Example:
for(Student__c student :
Trigger.new){
Student__c oldStudent =
Trigger.oldMap.get(
student.Id
);
if(
student.Status__c !=
oldStudent.Status__c){
System.debug(
'Status Changed'
);
}
}
Changes can be identified easily.
Status Changed
This is a common real-world use case.
Returns:
true
if the trigger is running because of an insert operation.
Otherwise:
false
if(
Trigger.isInsert){
System.debug(
'Insert Event'
);
}
Useful for event-specific logic.
Returns:
true
during update operations.
if(
Trigger.isUpdate){
System.debug(
'Update Event'
);
}
The trigger can identify update events.
Returns:
true
during delete operations.
if(
Trigger.isDelete){
System.debug(
'Delete Event'
);
}
Useful for deletion logic.
Returns:
true
when records are restored from the Recycle Bin.
if(
Trigger.isUndelete){
System.debug(
'Record Restored'
);
}
Supports restoration workflows.
Returns:
true
when running in a Before Trigger.
if(
Trigger.isBefore){
}
Developers can identify execution timing.
Returns:
true
when running in an After Trigger.
if(
Trigger.isAfter){
}
Useful for execution control.
Returns the number of records being processed.
Data Type:
Integer
System.debug(
Trigger.size
);
Output:
200
This indicates 200 records are being processed.
Business Requirement:
Assign Active status during insert and log updates.
Trigger:
trigger StudentTrigger
on Student__c
(
before insert,
after update
){
if(
Trigger.isInsert &&
Trigger.isBefore){
for(Student__c student :
Trigger.new){
student.Status__c =
'Active';
}
}
if(
Trigger.isUpdate &&
Trigger.isAfter){
System.debug(
'Student Updated'
);
}
}
The Trigger behaves differently depending on context.
| Variable | Insert | Update | Delete | Undelete |
|---|---|---|---|---|
| Trigger.new | Yes | Yes | No | Yes |
| Trigger.old | No | Yes | Yes | No |
| Trigger.newMap | Yes* | Yes | No | Yes |
| Trigger.oldMap | No | Yes | Yes | No |
*Available in After Insert.
This matrix helps developers choose the correct variable.
Example:
for(Student__c student :
Trigger.new){
}
All records should be processed together.
Never assume a single record.
Bulkification is mandatory.
Context Variables support all these scenarios.
These practices improve performance.
Developers should avoid these issues.
A software training company requires:
Trigger Context Variables make these requirements possible.
They provide complete awareness of trigger execution.
Understanding Trigger Context Variables helps professionals:
Context Variables are used in almost every Apex Trigger.
Trigger Context Variables provide information about the current trigger execution, including records, events, execution timing, and record changes. Through Trigger.new, Trigger.old, Trigger.newMap, Trigger.oldMap, Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete, Trigger.isBefore, Trigger.isAfter, and Trigger.size, developers can build dynamic, scalable, and efficient Apex Triggers. Mastering Context Variables is essential for professional Salesforce development.
They are system-provided variables that provide information about trigger execution.
Trigger.new contains the new versions of records being processed.
Trigger.old contains previous record values.
Trigger.newMap stores records in a Map using record IDs as keys.
It returns true when the trigger executes during an insert operation.
They allow developers to access record data and build event-specific business logic.
Looking to learn more technologies and programming skills?
WhatsApp us