function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gopal m 14gopal m 14 

two triggers

two different programs, two before triggers are there then which one execute first?
Tarun_KhandelwalTarun_Khandelwal
Triggers are executed on DML events of object and on the order in which they are operated. For more info, go through the link - 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
 
Amit Chaudhary 8Amit Chaudhary 8
You can controle the two trigger executaion. That is why salesforce recomment to create one trigger per object then you can call any apex function according to you need. Please check below post I hop that will help u
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

1) One Trigger Per Object
A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

Please let us know if this will help u
Vishal Negandhi 16Vishal Negandhi 16
Triggers are not something that are in our control. If I have 5 triggers on the same event for an object, I will never be able to control the order of execution of these triggers. 

So as suggested in the posts above, have one trigger one object. And use apex classes as handlers where all the logic stays. This way you can always re-order your methods in the apex class to control the order. 

Example:
My trigger:

Trigger sampleTrigger on Contact(after insert){
     ContactTriggerHandler cth = new ContactTriggerHandler();
     if(Trigger.isInsert && Trigger.isAfter){
           cth.OnAfterInsert(trigger.new);
     }
}

And my handler class:
public class ContactTriggerHandler{
     public ContactTriggerHandler(){
     }
     
     public void OnAfterInsert(List<Contact> newContacts){
     // call method 1 : 
     updateSomeFieldOnAccount();
     // call method 2 :
     updateSomeFieldOnOtherObjectBasedOnUpdateAbove();
     } 
}

So in my onafterinsert method above, I am calling two methods - each having some business logic. 
So idea is, I want to update some data based on some update I perform on Account. If I have two triggers then it may happen that the one which updates some object data may fire before the one that updates the Account. 

In apex, I can re-order my methods as per my requirement and hence this is a much better way of writing triggers. 

Hope this helps you :)