- July 5, 2018 at 6:30 am #29660Sanjana #13
When to use before vs after Triggers in Salesforce?
When to use before vs after triggers in Salesforce??July 30, 2018 at 11:05 am #30624
We use before triggers when we want to update any field or validate any record before they are saved to the database.After triggers are used when we wish to access any field values after they are saved to the database.Just remember, we cannot update a field value in after trigger because once they are saved to the database, values become read-only and we cannot make changes to them.September 14, 2018 at 4:42 am #34038Parul #1
Triggers can run BEFORE the action is taken or AFTER the action is taken.
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.
Example: Let’s say you run a database for a bank. You have a table accounts and a table transactions. If a user makes a withdrawal from his account, you would want to make sure that the user has enough credits in his account for his withdrawal. The BEFORE trigger will allow to do that and prevent the row from being inserted in transactions if the balance in accounts is not enough.
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).
Example: After a successful transaction, you would want balance to be updated in the accounts table. An AFTER trigger will allow you to do exactly that.
Thanks.September 14, 2018 at 6:35 am #34069shariq #2September 14, 2018 at 1:30 pm #34145
Please Register to reply to this topic. /