Trigger context variables in Salesforce Apex are collections of data that provide specific information about the context in which a trigger is executed. They help the trigger know things like what kind of change was made, which records were involved, and what the data looked like before and after the change. These variables are crucial for the trigger to perform the right actions based on what's happening in the database.
Trigger.New: This variable provides a list of new records when the trigger event is before insert, after insert, before update, after update, and after undelete.
Example:
trigger trigger_name on Account (before insert, before update) {
for (Account account : Trigger.new) {
if (account.Name.contains('Test')) {
account.Description = 'This is
a test account';
}
}
}
Trigger.Old: This variable provides a list of old records when the trigger event is before update, after update, before delete, and after delete.
Example:
trigger trigger_name on Opportunity (before update) {
for(Opportunity oldOpportunity : Trigger.old) {
Opportunity newOpportunity =
Trigger.newMap.get(oldOpportunity.Id);
if(newOpportunity.Amount !=
oldOpportunity.Amount) {
System.debug('Opportunity
Amount changed from ' + oldOpportunity.Amount + ' to ' +
newOpportunity.Amount);
}
}
}
Example:
trigger trigger_name on Opportunity (before update) {
Map<Id, Opportunity> newOpportunitiesMap = new Map<Id,
Opportunity>(Trigger.newMap);
for (Id opportunityId : newOpportunitiesMap.keySet()) {
Opportunity newOpportunity =
newOpportunitiesMap.get(opportunityId);
if (newOpportunity.Amount > 10000) {
newOpportunity.StageName = 'High
Value';
}
}
}
Trigger.oldMap: This variable provides a list of old records indexed by their IDs when the trigger event is before update, after update, before delete, and after delete.
Example:
trigger trigger_name on Opportunity (before update) {
for (Opportunity newOpportunity : Trigger.new) {
Opportunity oldOpportunity =
Trigger.oldMap.get(newOpportunity.Id);
if (newOpportunity.Amount !=
oldOpportunity.Amount) {
newOpportunity.Amount.addError('Amount cannot be changed);
}
}
}
Trigger.isInsert: This is a boolean variable that returns true when the trigger is fired due to an insert operation.
Example:
trigger trigger_name on Account (before insert, before update) {
if (Trigger.isInsert) {
System.debug('Trigger fired due to an insert
operation.');
} else {
System.debug('Trigger fired due to an operation
other than insert.');
}
}
Example:
trigger trigger_name on Account (before insert, before update) {
if (Trigger.isUpdate) {
System.debug('Trigger fired due to an update
operation.');
} else {
System.debug('Trigger fired due to an operation
other than update.');
}
}
Trigger.isDelete: This is a boolean variable that returns true when the trigger is fired due to a delete operation.
Example:
trigger trigger_name on Account (before delete) {
if (Trigger.isDelete) {
System.debug('Trigger fired due to a delete
operation.');
} else {
System.debug('Trigger fired due to an operation
other than delete.');
}
}
Trigger.isUndelete: This is a boolean variable that returns true when the trigger is fired after a record is recovered from the Recycle Bin.
Example:
trigger trigger_name on Account (after undelete) {
if (Trigger.isUndelete) {
System.debug('Trigger fired due to an undelete
operation.');
} else {
System.debug('Trigger fired due to an operation
other than undelete.');
}
}
Trigger.isBefore: This is a boolean variable that returns true when the trigger is fired before any record is saved.
Example:
trigger trigger_name on Account (before insert, before update) {
if (Trigger.isBefore) {
System.debug('Trigger fired before any record is
saved.');
} else {
System.debug('Trigger fired after record
saving.');
}
}
Trigger.isAfter: This is a boolean variable that returns true when the trigger is fired after all records are saved.
Example:
trigger trigger_name on Account (after insert, after update) {
if (Trigger.isAfter) {
System.debug('Trigger fired after all records are
saved.');
} else {
System.debug('Trigger fired before all records
are saved.');
}
}
Trigger.isExecuting: This is a boolean variable that returns
true if the code is running within a trigger context,
otherwise false.
Example:
trigger trigger_name on Account (before insert, before update) {
if (Trigger.isExecuting) {
System.debug('Code is executing within a trigger
context.');
} else {
System.debug('Code is not executing within a
trigger context.');
}
}