Activity Forums Salesforce® Discussions When do you use a before vs. after trigger?

  • Prachi

    Member
    September 13, 2018 at 1:38 pm

    hi,

    Before Trigger:
    In case of validation check in the same object.
    Update the same object.
    After Trigger:
    Insert/Update related object,not the same object.
    Notification Email.

    thanks

  • madhulika shah

    Member
    September 13, 2018 at 1:41 pm

    Hi Avnish,

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

    thanks.

  • Anurag

    Member
    September 13, 2018 at 1:44 pm

    Hi Avnish,

    Generally we go for Before event if there is something which needs to be validated before the actual data is committed to database.
    For example :
    Let suppose you want to Insert new Account, but before the date is committed 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".

  • shariq

    Member
    September 13, 2018 at 2:56 pm

    Hi,

    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 LastModifiedDatefield), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.

    Hope this helps!

  • Parul

    Member
    September 13, 2018 at 4:04 pm

    Hi,

    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.

Log In to reply.

Popular Salesforce Blogs