+ Start a Discussion
learn_cloudsflearn_cloudsf 

before and after triggers

 what exactly is the difference between before and after triggers ? Please can someone explain with real time example.
karthikeyan perumalkarthikeyan perumal
Hello, 

kinldy refer following things 

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.

    In case of validation check in the same object.
    Update the same object.
   

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

    Insert/Update related object, not the same object.
    Notification email.


Note before using trigger kinldy consider Salesforce Order of Excution.  when you have to use before and after trigger. 


Here is the standard Salesforce.com order of execution

Old record loaded from database (or initialized for new inserts)
New record values overwrite old values
System Validation Rules
All Apex “before” triggers
Custom Validation Rules
Record saved to database (but not committed)
Record reloaded from database
All Apex “after” triggers
Assignment rules
Auto-response rules
Workflow rules
Escalation rules
Parent Rollup Summary Formula value updated (if present)
Database commit
Post-commit logic (sending email)

Hope this will Help you.. 

Mark Best ANSWER if its clear. 

Thanks
karthik