3.6 The difference between before and after trigger

In the previous example, we have used after trigger. Have you ever wondered what are the differences between before and after triggers?

Well, in most cases, you can use them interchangeably. But there are certain differences you might need to be aware of.

Before trigger:

The key thing to remember is that in before triggers, the records are not changed in the database yet. So if it is a before insert trigger, you can’t query those records. Some thing like:

List<Account> accList = [Select Id, Name From Account Where Id in :Trigger.New];

won’t work in before insert trigger because the records are not there yet. But it will work in before update trigger.

And things like formula fields won’t be available in before insert triggers as well.

The major advantage of before trigger, is you can modify the values of Trigger.New. And by doing so you don’t need an additional DML to modify the data. So if you need to modify the values in Trigger.New, before trigger is usually a better option.

After trigger:

In after trigger, the records are already changed in the database. So you can query the records. But since the data manipulation is already done. You can’t modify the value in Trigger.new.

Instead, if you still need to modify the values, you will have to generate a new List. Something like below:

List<Account> accList = new List<Account>();

for(Account acc: Trigger.New)


Account newAcc = new Account();

newAcc.Id = acc.Id;

newAcc.Name = acc.Name + 'Test';



update accList;

It is less convenient than the way you accomplish the same functionality in before trigger. However, sometimes you still have to do so.

When to use which:

As I said before, most times, they are pretty much interchangeable. But there are still preferences in certain situations. The below part is coming from Salesforce Stackexchange. Original post is here.


  • I’m updating the record that’s being updated/inserted – or doing something based on the record being modified
  • Examples: Set value of a pick list based on criteria. Send apex e-mail based on the record updated/inserted


  • I’m updating or creating records that are NOT being updated/inserted
  • Examples: Create a task of an Opportunity that’s been edited, Change a look up value on a related record from the Opportunity being edited

Next Post

4.1 Write an advanced trigger

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 


  1. Jeffery - August 3, 2017 @ 8:35 am

    This is a very good suggestions particularly to those new to blogosphere, short and accurate advice…
    Thanks for sharing this one. A must read article.

  2. Esmeralda - August 5, 2017 @ 11:43 pm

    Thanks for the excellent information, it really is useful.

Leave a Reply

Your email address will not be published / Required fields are marked *