function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chaitra GVChaitra GV 

Difference between before and after trigger

If we want to update a field, for example case.status, which trigger is used? before or after insert? My understanding is that, "after insert" will be used since we are updating a field after the record is inserted.
Is that correct? Please help.
Raj VakatiRaj Vakati
Before triggers are used to update or validate record values before they’re saved to the database.

After triggers are 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 trigger are read-only.


you need to use before .. after insert record will be rad only 

if you use before you no need to perform dml.. assigning field value is enough 


 
Murali MattaMurali Matta
hi Chaitra,

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.

Hope below links can help you.

https://salesforce.stackexchange.com/questions/96263/what-is-the-exact-difference-between-before-update-and-after-update-trigger
http://sfdcinpractice.com/index.php/2017/01/09/3-6-difference-trigger/


Let me know if you have any confusion.

Kindly mark this as solved if the reply was helpful.

Thanks,
Murali
Deepali KulshresthaDeepali Kulshrestha
Hi Chaitra,

A trigger is the piece of code that executed before and after a record is Inserted/Updated/Deleted from the force.com database.

Before Trigger: Before triggers are used to perform the logic on the same object and specifically we cannot use the DML operation (Insert, update, delete) on these triggers. These triggers fired before the data saved into the database.

After Trigger: After triggers are used to perform the logic on the related objects and these triggers are used access the fields values that are created by system (Ex: CreatedBy, LasteModifiedBy , Record Id etc..).

For more information please refer to these links:
https://www.salesforcetutorial.com/apex-trigger-in-salesforce/    
http://sfdcinpractice.com/index.php/2017/01/09/3-6-difference-trigger/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Gary RalstonGary Ralston
Hi Chaitra,

The main difference between before and after triggers is the timing of when the trigger is executed. To update a field, the trigger used would depend on the specific requirements of your use case. Both before insert and after insert triggers can be used to update the case.status field.
  • Before triggers are executed before the record is saved to the database,If you want to update a field value in a before trigger, you can modify the value directly in the trigger.new context variable.
  • After triggers are executed after the record has been saved to the database, If you want to update a field value in an after trigger, you need to perform a DML operation (such as update) on the record.
For a more in-depth explanation on triggers, check out the article at https://arrify.com/triggers-in-salesforce/. This guide provides a detailed explanation of triggers in Salesforce and is a great resource for anyone looking to learn more about this important aspect of the platform.