In Salesforce, to automate processes such as updating records, sending emails, or assigning tasks, we use Salesforce Flows.
However, there are sometimes complex requirements that Flows cannot handle directly, such as making multiple record updates in bulk, performing complex validations, or working with related records.
In such cases, we use Apex Triggers to write custom code and automate those processes. When writing triggers, we need to specify when we want the trigger to run, whether before or after the DML operation occurs.
In this article, we will explore before and after Triggers in Salesforce, including when to use them, why they are important, and provide real-world examples.
Before vs After Triggers in Salesforce
Below, I will explain what Before and After Triggers are in Salesforce, when to use them, and how to write triggers with real-time examples.
Before Triggers in Salesforce
First, we will understand the Before Trigger, when to use it, and see an example.
A Before Trigger in Salesforce runs before the record is saved into the database.
It is mainly used to validate data or update field values on the same record before it is committed.
It is mainly used when:
- You want to validate data (e.g., prevent wrong values from being saved).
- You want to update fields on the same record before saving.
- You need to set default values automatically.
Let’s take an example: We have created an account record in the account object. If we attempt to change the account number field value of an existing account record before the record gets updated, we need to display an error message. Using this, we can validate data before it is saved to the Salesforce object.
We checked the old and new account record IDs in the Apex trigger below. If the new account number is not equal to the old one, it is changed. Here, we want to prevent the record from being saved before it is.
For that, we added the addError() method, which displays error messages in Salesforce UI and prevents records from being saved. Provide the message that you want to display when the error occurs.
After that, save the Apex trigger code to activate it.
trigger PreventFieldChange on Account ( before update ) {
for ( Account oldAcc : Trigger.Old ) {
for ( Account newAcc : Trigger.New ) {
if ( oldAcc.Id == newAcc.Id &&
oldAcc.AccountNumber != newAcc.AccountNumber) {
newAcc .addError ( 'You cannot change the Account Number.' );
}
}
}
}Navigate to the account object tab and open any existing account record in edit mode. Then, change the account number and enter a different value. Click the Save button to save changes.

As you click the save button, you will see an error message before the record is saved, which relates to the record you provided in the addError() method.

In this way, we can use the before trigger and the addError() method in Salesforce to display a custom error message at the Bottom of the record page.
After Triggers in Salesforce
Now, let’s understand what triggers are in Salesforce, when to use them, and how to write triggers with real-time examples.
An After Trigger runs after the record is saved in the Salesforce database. That means that after the record is saved in the object, any actions we want to perform will be executed in the after trigger in Salesforce.
It is mainly used when:
- You need the record ID (for creating related records).
- You want to update or insert related records (e.g., Tasks, Cases, Contacts).
- You want to perform actions that depend on the saved data.
Let’s take an example: when a new contact record is created and the lead source is ‘Web’ from the contact record, create a new task record and assign the newly created task to the contact owner.
As we want to create new task records when contact records are created, we need to create a trigger on the contact object. We also need to use the after-insert trigger event. First, we created a list collection of task objects, which will store the values of the task objects’ fields.
Then, in the for each loop, we created a contact object instance and assigned a Trigger.New to that instance, so that whenever a new record is created on the contact object, it will get assigned to the contact object instance.
Then, in the if statement, we added the condition that if the lead source of the contact record is the web, then only process to the next step.
If the lead source is the web, then in the next step, we add the information of task object fields and also assign that task to the contact owner themselves.
Finally, we created a record for the task object using the Insert DML operation. Save the trigger and then navigate to the contact object to verify whether the task is created after a contact is created.
trigger ContactTrigger on Contact ( after insert ) {
List<Task> tasksToCreate = new List<Task>();
for ( Contact con : Trigger.New ) {
if ( con.LeadSource == 'Web' ) {
tasksToCreate.add (new Task (
Subject = 'Follow-up with web contact',
Status = 'Not Started',
Priority = 'High',
OwnerId = con.OwnerId
));
}
}
if ( !tasksToCreate.isEmpty() ) {
insert tasksToCreate;
}
} Here, I’m creating the contact record with the lead source as ‘Web’ and the contact owner as ‘Alex Kirk’. Now, when this record gets created, the task record should also be created and assigned to the contact owner.
Click the Save button to create the contact record.

When the contact record is saved, navigate to the Task object to verify whether the new task is created with the values provided in the trigger code.
In the image below, you can see that the task record has been created with the same values we provided, and it has also been assigned to the contact owner.

In this way, we can use the after trigger and the Trigger.New with the trigger event in Salesforce Apex.
Conclusion
I hope you have got an idea about before and after Triggers in Salesforce, when to use them, and how to write them with real-time examples. If you practice these scenarios, you will easily understand the difference and know which trigger to use in your projects.
You may like to read:
- Write a Test Class For an Apex Trigger in Salesforce
- Trigger Context Variable in Salesforce Apex
- Trigger Handler Pattern in Salesforce Apex
- CANNOT EXECUTE FLOW TRIGGER Error With DML Operation in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.