Curriculum
Before Triggers are Apex Triggers that execute before records are saved to the Salesforce database. They are commonly used to validate data, modify field values, enforce business rules, populate default values, and prevent invalid records from being saved.
Because Before Triggers run before the database operation occurs, developers can modify record values directly without performing additional DML operations. This makes Before Triggers highly efficient and one of the most frequently used Trigger types in Salesforce development.
Understanding Before Triggers is essential for Salesforce Developers because they are widely used in data validation, automation, and business process implementation.
A Before Trigger executes before Salesforce saves a record to the database.
The Trigger runs during:
At this stage:
Record Not Yet Saved
Developers can modify values before storage.
Organizations often need to:
Before Triggers perform these actions before data is committed.
Directly modify records.
Reduce database operations.
Ensure accurate information.
Prevent invalid records.
Maintain consistency.
These benefits make Before Triggers extremely valuable.
Salesforce supports three Before Trigger events.
Each event serves a different purpose.
A Before Insert Trigger executes before a new record is saved.
Example Uses:
The record does not yet exist in the database.
trigger StudentTrigger
on Student__c
(before insert) {
}
This Trigger executes before Student records are created.
Business Requirement:
Every new student should have an Active status.
Trigger:
trigger StudentTrigger
on Student__c
(before insert) {
for(Student__c student :
Trigger.new){
student.Status__c =
'Active';
}
}
The Status field is populated automatically.
Input:
Name:
Rahul Sharma
Output:
Name:
Rahul Sharma
Status:
Active
No additional DML operation is required.
A Before Update Trigger executes before an existing record is updated.
Example Uses:
The updated record has not yet been saved.
trigger StudentTrigger
on Student__c
(before update) {
}
This Trigger executes before updates occur.
Business Requirement:
Student names should always be stored in uppercase.
Trigger:
trigger StudentTrigger
on Student__c
(before update) {
for(Student__c student :
Trigger.new){
student.Name =
student.Name.toUpperCase();
}
}
The name is automatically standardized.
Input:
Rahul Sharma
Output:
RAHUL SHARMA
Data consistency improves.
A Before Delete Trigger executes before records are deleted.
Example Uses:
Developers can stop deletion when necessary.
trigger StudentTrigger
on Student__c
(before delete) {
}
The Trigger runs before removal.
Business Requirement:
Students with active enrollments cannot be deleted.
Trigger:
trigger StudentTrigger
on Student__c
(before delete) {
for(Student__c student :
Trigger.old){
student.addError(
'Student cannot be deleted.'
);
}
}
Deletion is blocked.
Output:
Student cannot be deleted.
The record remains in Salesforce.
Trigger.new contains new versions of records being inserted or updated.
Example:
for(Student__c student :
Trigger.new){
}
Used in:
Developers can modify values directly.
for(Student__c student :
Trigger.new){
student.Status__c =
'Active';
}
The field value changes before saving.
Trigger.old contains previous versions of records.
Example:
for(Student__c student :
Trigger.old){
}
Used in:
Developers can compare old and new values.
for(Student__c oldStudent :
Trigger.old){
}
Useful for validation logic.
Business Requirement:
Student age must be at least 18.
Trigger:
trigger StudentTrigger
on Student__c
(before insert,
before update) {
for(Student__c student :
Trigger.new){
if(student.Age__c < 18){
student.addError(
'Age must be 18 or above.'
);
}
}
}
Invalid records are rejected.
Output:
Age must be 18 or above.
The record is not saved.
Business Requirement:
Completed courses cannot be modified.
Trigger:
trigger CourseTrigger
on Course__c
(before update) {
for(Integer i = 0;
i < Trigger.new.size();
i++){
if(
Trigger.old[i]
.Status__c
== 'Completed'){
Trigger.new[i]
.addError(
'Completed courses cannot be updated.'
);
}
}
}
Business rules are enforced automatically.
Example:
trigger FeeTrigger
on Student__c
(before insert,
before update) {
for(Student__c student :
Trigger.new){
student.Total_Fee__c =
student.Course_Fee__c +
student.Registration_Fee__c;
}
}
The total fee is calculated automatically.
Example:
trigger StudentTrigger
on Student__c
(before insert,
before update) {
for(Student__c student :
Trigger.new){
student.Email__c =
student.Email__c
.toLowerCase();
}
}
All email addresses are stored consistently.
Triggers must handle multiple records.
Example:
for(Student__c student :
Trigger.new){
}
Never assume only one record is processed.
Bulkification is essential.
These are among the most common Salesforce automation scenarios.
These advantages make Before Triggers highly efficient.
Developers must understand these limitations.
These practices improve scalability.
Developers should avoid these issues.
A software training company requires:
A Before Trigger can perform all these tasks before records are saved.
This improves data quality and reduces manual work.
Understanding Before Triggers helps professionals:
Before Triggers are among the most frequently used Trigger types.
Before Triggers execute before Salesforce records are saved to the database. Through Before Insert, Before Update, and Before Delete events, developers can validate data, enforce business rules, standardize information, populate default values, and prevent invalid operations. Because records can be modified directly without additional DML statements, Before Triggers provide high-performance automation and are a fundamental part of Salesforce development.
A Before Trigger executes before a record is saved to the Salesforce database.
Before Insert, Before Update, and Before Delete.
They allow direct record modification without additional DML operations.
Trigger.new contains the new versions of records being inserted or updated.
Trigger.old contains previous versions of records during updates and deletions.
Data validation, default values, calculations, and business rule enforcement.
Looking to learn more technologies and programming skills?
WhatsApp us