Activity Forums Salesforce® Discussions Sequence of Execution of Triggers on "before insert"

  • Sequence of Execution of Triggers on "before insert"

    Posted by Suraj on April 24, 2017 at 12:59 pm

    If one object in Salesforce have 2 triggers which runs “before insert”. Is there any way to control the sequence of execution of these triggers?

    Saurabh replied 6 years, 12 months ago 3 Members · 2 Replies
  • 2 Replies
  • Radhakrishna

    Member
    April 25, 2017 at 11:20 am

    Hello Suraj,

    Triggers get a list of objects (up to 200), but process each object individually. The before trigger as a whole will complete its batch before the after trigger fires. However, if you have more than 200 objects, there will be multiple before triggers (1 trigger context for each batch of up to 200 objects). When each trigger context finishes, the records in that trigger become eligible for after triggers. In your after triggers, you can't be certain that it is the same set of 200 objects that were in your original before trigger since a user may have created/updated a record which would get included in the trigger batch.

    As an alternative to triggers, have you thought about using Workflow Rules and Field Updates/Email Alerts? Those are both non-programming approaches.

  • Saurabh

    Member
    April 25, 2017 at 1:16 pm

    Hi Suraj

    If you write two triggers, there is no particular order of execution and one might run first and sometimes second. In order to control the triggers order of execution, many experts have built Trigger Templates,that basically allow developers to control the order of execution and also control when the trigger needs to be fired. Please take a look at the below blog, you can even search for "Trigger Template" and get many other posts

    http://www.tgerm.com/2012/01/salesforce-apex-trigger-template.html

    Hope it may help

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos