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.
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, specifically.
example, if a before trigger is written on Contact and you want to change a value of one of the fields it would be
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,
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/) 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.
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:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
https://trailhead.salesforce.com/en/apex_triggers/apex_triggers_intro
Please mark this as best answer if your query is resolved, so it will help others in future.
Thanks,
Gaurav Jain
When to use “before” vs “after” triggers----->http://www.salesforcetutorial.com/apex-trigger-in-salesforce/
http://www.sfdc99.com/2014/01/25/use-vs-triggers/
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,
specifically.
example, if a before trigger is written on Contact and you want to change a value of one of the fields it would be
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,
Thanks,
David(1040)
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/)
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.
https://trailhead.salesforce.com/modules/apex_triggers/units/apex_triggers_intro
I hope it will be helpful.
Please mark as best answer if it helps you.
Thank you.
Akshay