Before vs After Trigger — What Actually Matters

In the last post, we looked at a simple Apex Trigger. Now comes the part that confuses almost everyone at the start — before vs after triggers.

Both run automatically, both look similar… but they are used for completely different purposes. Once you understand this, your trigger logic becomes much cleaner.

Before Trigger

A before trigger runs before the record is saved to the database. This is where you prepare or validate your data.


trigger AccountBeforeTrigger on Account (before insert) {
    for (Account acc : Trigger.new) {
        if (acc.Name != null) {
            acc.Name = acc.Name.toUpperCase();
        }
    }
}
  

Here, we are modifying the record directly before it gets saved. No extra DML is needed — Salesforce handles the save automatically.

After Trigger

An after trigger runs after the record is saved. At this point, the record already exists in the database and has an ID.


trigger AccountAfterTrigger on Account (after insert) {
    List<Contact> contacts = new List<Contact>();

    for (Account acc : Trigger.new) {
        contacts.add(new Contact(
            LastName = acc.Name,
            AccountId = acc.Id
        ));
    }

    insert contacts;
}
  

Here, we create related records using the Account ID, which is only available after insert.

So what’s the real difference?

Think of it like this:

Common mistakes I see

One simple rule

If you’re updating the same record → use before
If you’re working with related data → use after

That’s it. Keep it simple and you’ll avoid most trigger issues. In real projects, this clarity makes a big difference.

← Back to Apex