Trigger Events
What Are Trigger Events?
Trigger events define when the Apex trigger code should run in relation to the DML (Data Manipulation Language) operations like insert, update, or delete.
Salesforce supports 7 main events for triggers.
List of Trigger Events
Event | Description |
---|---|
before insert | Runs before a new record is saved to the database |
after insert | Runs after a new record is saved |
before update | Runs before changes to a record are committed |
after update | Runs after the record is saved with changes |
before delete | Runs before a record is deleted |
after delete | Runs after the record is deleted |
after undelete | Runs after a record is restored from the Recycle Bin (undeleted) |
Event Examples :
Example 1: before insert
trigger SetDefaultRating on Lead (before insert) {
for (Lead l : Trigger.new) {
if (String.isBlank(l.Rating)) {
l.Rating = ‘Warm’;
}
}
}
Explanation :
•This trigger runs before a new Lead record is saved to the database.
• If the Rating field is empty, it sets it to “Warm”.
• Use Case: Set a default value before the record is saved.
2. After insert :
trigger CreateTaskForNewContact on Contact (after insert) {
List<Task> tasks = new List<Task>();
for (Contact c : Trigger.new) {
tasks.add(new Task(
Subject = ‘Welcome ‘ + c.FirstName,
WhatId = c.Id,
Status = ‘Not Started’
));
}
insert tasks;
}
Explanation :
• Runs after new Contact records are saved.
• Runs after an Opportunity is updated.
• Logs a message if the stage has changed.
• after update is used when we don’t need to change the record, just log or create something else.
•Use Case: Audit logs, sending notifications, etc.
3. Before update:
trigger BlockStatusChange on Case (before update) {
for (Case c : Trigger.new) {
Case oldCase = Trigger.oldMap.get(c.Id);
if (oldCase.Status == ‘Closed’ && c.Status != ‘Closed’) {
c.addError(‘Closed cases cannot be reopened.’);
}
}
}
Explanation :
• Runs before a Case is updated.
• If a Case was “Closed” and someone tries to change the status, it throws an error.
• Use Case: Prevents updates that are not allowed.
4. After insert :
trigger LogOpportunityStageChange on Opportunity (after update) {
for (Opportunity opp : Trigger.new) {
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
if (oldOpp.StageName != opp.StageName) {
System.debug(‘Stage changed from ‘ + oldOpp.StageName + ‘ to ‘ + opp.StageName);
}
}
}
Explanation :
• Runs after an Opportunity is updated.
• Logs a message if the stage has changed.
• after update is used when we don’t need to change the record, just log or create something else.
• Use Case : Audit logs, sending notifications, etc.
5. before delete :
trigger PreventContactDelete on Contact (before delete) {
for (Contact c : Trigger.old) {
if (c.Email != null) {
c.addError(‘You cannot delete a contact that has an email address.’);
}
}
}
Explanation :
This trigger runs before a Contact record is deleted.
It checks if the contact has an email address.
If yes, it blocks the deletion and shows an error message:
“You cannot delete a contact that has an email address.”
6. After delete :
trigger LogDeletedContacts on Contact (after delete) {
for (Contact c : Trigger.old) {
System.debug(‘Deleted Contact: ‘ + c.FirstName + ‘ ‘ + c.LastName);
}
}
Explanation :
• Runs after Contacts are deleted.
• Logs the deleted contact names for tracking.
• Use Case: Logging or external syncing after deletion.
7. after undelete :
trigger RestoreLog on Contact (after undelete) {
for (Contact c : Trigger.new) {
System.debug(‘Contact record restored: ‘ + c.FirstName + ‘ ‘ + c.LastName);
}
}
Explanation :
• This trigger runs after a Contact record is undeleted (restored from the Recycle Bin).
• It logs the names of the restored Contacts.
• Use Case : Logging, re-activating related data, or sending notifications when a record is brought back.
Practice Tasks :
1-Write a trigger to set the LeadSource of a new Lead to “Web” if it’s left blank.
2-Write a trigger to prevent deletion of any Account whose Type is “Customer”.
3-Write a trigger to update the Status of a Case to “In Progress” if it is “New” when updated.
4-Write a trigger to create a Task when a new Contact is added.
5-Write a trigger that logs (debugs) when a Contact is undeleted.