+ Start a Discussion
Anitha Reddy 14Anitha Reddy 14 

What is the difference between before trigger and after trigger??

Gaurav Jain 103Gaurav Jain 103
HI Anita,

Before triggers can be used to update or validate record values before they are saved to the database.

After triggers can be used to access field values that are set by the database (such as a record's Id or lastUpdated field) and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue.

Use Before Trigger:
In the case of validation check in the same object.
Insert or update the same object.

Use After Trigger:
Insert/Update related object, not the same object.
Notification email.
We cannot use After trigger if we want to update a record because it causes read only error. This is because, after inserting or updating, we cannot update a record.

Please see the Useful links:


Please mark this as  best answer if your query is resolved, so it will help others in future.

Gaurav Jain
Naveen DhanarajNaveen Dhanaraj
I hope this link will give you the best Examples and Explanation,

When to use “before” vs “after” triggers----->http://www.salesforcetutorial.com/apex-trigger-in-salesforce/

David HalesDavid Hales
HI Anita,

Whenever Save a record in salesforce instance.Salesforce fires a DML statement to its database either insert or update, internally.

1. All the code written in the "before update" triggers, executes BEFORE that DML is committed to Salesforce Database.
2. The code is written in after trigger executes AFTER the commit is made.

Hence if you are trying to update any value in the record, on which the trigger is fired, you need not write an update call, 

example, if a before trigger is written on Contact and you want to change a value of one of the fields it would be
for(Contact eachContact: Trigger.new)
    eachContact.Value__c = 'New value';

but if you will write this on after update you can't update the same field because data is already committed to database
so value will be read-only.

After update trigger generally works when you want to update any other object. 
for example, if you want to create a new task on Contact insertion or update you can write logic for that.

In before insert context your Trigger.NewMap always be null because in before context records is not submitted to database so Id is not generated that's why in before insert we don't use  Trigger.NewMap

But in After insert Id is generated so We can use Trigger.NewMap

In case of before and After update since id is already generated in Insert event.

SO we can use Trigger.NewMap in before and After update

Hope It will help you to understand main differences,

Hi Anitha Reddy,

Try the below code.

Before triggers:  used to update or validate record values before they’re saved to the database.
After triggers: used to access field values that are set by the system (such as a record's Id or LastModifiedDate field) and to effect changes in other records. The records that fire the after the trigger is read-only.
We cannot use After trigger if we want to update a record because it causes a read-only error. This is because after inserting or updating, we cannot update a record.

Please check the below link for an understanding of Triggers.
https://developer.salesforce.com/forums/?id=906F0000000AafFIAS (http://www.sfdc99.com/2014/01/25/use-vs-triggers/)
Please check the Trailhead module on the trigger so that you can accelerate on trigger concepts one of the best ways to learn the Salesforce.
Please check the link below.


I hope it will be helpful.

Please mark as best answer if it helps you.

Thank you.