Activity Forums Salesforce® Discussions Is There Any Way To Control The Sequence Of Execution Of These Triggers?

  • Is There Any Way To Control The Sequence Of Execution Of These Triggers?

    Posted by Aman on September 20, 2018 at 3:08 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?

    Aditya replied 3 years, 2 months ago 5 Members · 6 Replies
  • 6 Replies
  • shariq

    Member
    September 20, 2018 at 3:11 pm

    Hi,

    Salesforce.com has documented that trigger sequence cannot be predefined. As a best practice create one trigger per object and use comment blocks to separate different logic blocks. By having all logic in one trigger you may also be able to optimize on your SOQL queries.

    Thanks

  • Parul

    Member
    September 20, 2018 at 6:21 pm

    Adding some points :
    The following is the order Salesforce logic is applied to a record.

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

    Additional notes: There is no way to control the order of execution within each group above.
    Thanks

  • shariq

    Member
    September 20, 2018 at 10:36 pm

    Hi,
    Trigger example -

    trigger Duplicate Name on Contact (before insert, before update)
    {
    set<String> lastName = new set<String>();
    set<String> setname = new set<String>();
    for(Contact con : Trigger.new)
    {
    lastName.add(con.email);
    }
    for(Contact con : [select lastName from contact where email in : lastName])
    {
    setname.add(con.lastName);
    }
    if(Trigger.isInsert||Trigger.isUpdate)
    for(contact a:trigger.new)
    {
    if(setname.contains(a.lastName))
    {
    a.lastName.adderror('This email already exists');
    }
    }
    }

    Hope this helps.

  • Parul

    Member
    September 21, 2018 at 3:25 am

    Note:

    The order of execution isn’t guaranteed when having multiple triggers for the same object due to the same event. For example, if you have two before insert triggers for Case, and a new Case record is inserted that fires the two triggers, the order in which these triggers fire isn’t guaranteed.

     

    Thnks

  • Neha Talyan

    Member
    February 4, 2021 at 3:30 pm

    I was exploring and identified, it is possible if we instantiate the Apex class from trigger but you cannot control which trigger will execute first if they are on same events. But i am not sure how to do this.

  • Aditya

    Member
    February 19, 2021 at 2:24 pm

    Hi Neha,
    To control the sequence of execution of multiple context of a trigger is by using trigger factory

Log In to reply.

Popular Salesforce Blogs

Steps to Connect Pardot to Salesforce

Steps to Connect Pardot to Salesforce

Blog in

Steps to Integrate Salesforce and Pardot: There are some steps to install Pardot in salesforce 1- Click on this link https://login.salesforce.com/?ec=302&startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t1W000000U7P8(production) https://test.salesforce.com/?ec=302&startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t1W000000U7P8 (sandbox) 2- After…