+ Start a Discussion
viswsanathviswsanath 

whenever we can write 2 triggers on one account it is possible, if possible how to run 1st trigger,2nd trigger?

HI All,

whenever we can write 2 triggers on one account it is possible, if possible how to run 1st trigger,2nd trigger? pls explain me .

Thanks,
Viswa
sandeep sankhlasandeep sankhla
Hi Vishwa,

Hi , as a best practise we should always write one trigger for one object..you can create different different methods for events and use them in on trigger only if you need...I will share an example which will explain you how to use..

Example: In this example you can merge everything in one trigger only...

Create a Trigger for all events:

Trigger testTrigger on Account
{
       //create a handler class for this trigger
       AccountTriggerHandler objHandler = new AccountTriggerHandler ();

      if(trigger.isAfter && trigger.isUpdate)
      {
           objHandler .onAfterUpdate(trigger.new, trigger.old);
       }
}



create a class which will be a handler class for this

public class AccountTriggerHandler
{
      public AccountTriggerHandler ()
      {
           //main constructor
        }

       //define a method which will call from trigger
      public void onAfterUpdate(list<Account> lstOldAcc, list<Account> lstnewAcc)
     {
          //from this method you call all your methods which should execute after update

           updateCurency(lstOldAcc, lstnewAcc);
           updatePhone(lstOldAcc, lstnewAcc);
      }

   //here you can define those methods
   public void updateCurency(list<Account> lstOldAcc, list<Account> lstnewAcc)
   {
       //write your logic
    }

    public void updatePhone(list<Account> lstOldAcc, list<Account> lstnewAcc)
   {
       //write your logic
    }
}


like I define above, you can define many methods and call them from same event like for update I showed

Please mark this as best answer if it solves your doubt..let me know if you need anything else
 
James LoghryJames Loghry
To elaborate on Sandeep's answer, there's no guarantee that one trigger will fire before or after another trigger.  The way to solve this is with the single trigger.  A popular trigger handler pattern that many of us use can be found here: https://github.com/kevinohara80/sfdc-trigger-framework

Alternatively to triggers, you may implement your logic using Salesforce's new process builder.  Process builder is a fantastic new tool that can do most anything a trigger can do, but declartively or without code.  If you need to customize the process a little bit, you can still utilize  flow / Apex as needed.  

Similar to the trigger handler pattern, you can also use a single process, but dictate when your logic is fired as well.