Before vs After Triggers in Salesforce: When to Use & Why

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.

Use Before Trigger in Salesforce

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.

Salesforce Trigger Before Save

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&