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
SoundarSoundar 

Context Use Explaination

Dear Friends,

Could anyone give a Clear explaination the main difference between before insert & Update , after Insert & Update with good Example.

Regards,

Soundar.
Shruti SShruti S
Let me give it a whirl -

Before Insert
Before Insert triggers are fired before a record is saved into the database. This is usually used when we have some validations to do before a record is created in the Org. In this case, the records will still not be saved into the database. 

Eg: Imagine you have an Employee__c object which contains all the employees of a company. You can write a before insert trigger to prevent inserting employees with the same name. So if the Org has an employee named 'John Doe', and and now we are creating a new record with the same name as 'John Doe', then the trigger would be fired (as its creating/inserting a new record) and an error would be displayed.

Note: In before insert triggers, the records would not have an id as they are still not inserted into the database.

Before Update
Before Update triggers are fired when we are updating a record but the changes have not been saved yet.

Eg: Now say in the Employee__c object you have two records with names 'John Doe' and 'Angel Doe'. Suppose you are updating the 'Angel's record and changing its name to 'John', then on this the before update trigger will be fired and it will prevent users from updating the record with something that is not allowed.

After Insert / After Update
After Insert triggers are fired after the records are saved into the database. This is useful when we want to do something after a record is created/updated.

Eg: Imagine you have an object called Book__c in which you have fields called Price in INR which is entered in INR. Now you have an other field called Price in USD in which you want to show the price in USD. Then you can call a currency conversion API, get the rates in USD and update the Price in USD field. An other example is creating a record in an other object. Suppose we have two objects Employee__c and Interviewer__c, then whenever we create a record in the Employee__c object, a record will be created in the Interviewer__c object also.
NagendraNagendra (Salesforce Developers) 
Hi Sounder,

Before trigger
BEFORE triggers are usually used when validation needs to take place before accepting the change. They run before any change is made to the database.

After trigger
AFTER triggers are usually used when information needs to be updated in a separate table due to a change.
They run after changes have been made to the database (not necessarily committed).

BEFORE triggers should be used in below scenarios

Custom validation checks in the same object
Update the same record/object
Setting default values

AFTER triggers should be used in below scenarios

If we need to use Record's ID
Inserting or Updating the related records
To send notification email post-commit

For more information please check with below links which might help you.
http://sfdcinpractice.com/index.php/2017/01/09/3-6-difference-trigger/
https://hisrinu.wordpress.com/2011/05/17/difference-between-before-trigger-and-after-trigger/ (http://sfdcinpractice.com/index.php/2017/01/09/3-6-difference-trigger/)

Hope this helps.

Thanks,
Nagendra
SoundarSoundar
Hi Shruthi,

Really Thanks for your Clear Explaination (I understood your example explaination as well).

Regards,
Soundar,
+91-7418425418


 
SoundarSoundar
Hi Nagendra,

Really Thanks for your quick response and good explaination, I hope your link's should be helps to understand the Before,after Insert & update.

Regards,
Soundar,
+91-7418425418