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
SFDC_DevloperSFDC_Devloper 

Before and After events in trigger?

Hi All,

   
    Can any one Explain me Before and After events in trigger?When we are going for Before and when we are going for After in Triggers?


Thanks

hitesh90hitesh90
Hello Rock,

see below is the difference between before and after trigger.

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.

BEFORE 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).

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in/
Amit_Amit_
Hi, 
Generally we go for Before event if thier is something needs to be validated before the actual data is commited to database. 
For example : 
Let suppose you want to Insert new Account, but before the date is commited to database you want to validate if the Billing city is not null, if Null you will throw error and or else you will Insert record.
And in case of After, once the record is inserted/ Updated or Deleted, based on some condition you want to do some process.
For example:
Once the record is Inserted you want to send mail to update some filed on object "XYZ".

For refference please check this links :
http://www.sfdc99.com/2014/01/25/use-vs-triggers/
http://hisrinu.wordpress.com/2011/05/17/difference-between-before-trigger-and-after-trigger/ 
 
Regards,
Amit 
Virendra ChouhanVirendra Chouhan
Hy Rockzzz,

This Link will help you 
http://www.sfdc99.com/2014/01/25/use-vs-triggers/

Viru
srlawr uksrlawr uk
The answers here are pretty spot on so far, so hopefully that helps clear things up for you, if you do have a technical grasp of the construction of a single request in Salesforce, can I recommend you read the "order of execution" document here:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

You'll see that before triggers are fired at step 3 and the after triggers at step 6 - and then lots of other stuff you might have encountered happens, like workflow and email messages etc.etc.

You will want to also note that field updates in workflow can cause your triggers to be fired again if they update the record at step 11.

Being aware of this order of execution is crucial to developing effective triggers on the force.com platform :)
Abi DuthuAbi Duthu
Hi,

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 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.
anusha c 8anusha c 8
BEFORE triggers should be used in below scenarios
  1. Custom validation checks in the same object
  2. Update the same record/object
  3. Setting a default values
     AFTER triggers should be used in below scenarios
  1. If we need to use Record's ID
  2. Inserting or Updating the related records
  3. To send notification email post commit
anusha c 2anusha c 2
plz Check my blog for all interview Questions
https://anusalesforce.blogspot.in/