0

Just want a sanity check..

I have two methods on the same Trigger (both are isBefore execution).

Does it matter which order I execute them in the trigger? Or do they both execute at the same time?

Reason I ask is -- I have one method that runs a validation and present an error and the other updates related records ALL on the same record. Would this all happen at the same time ?

12

Apex Code is single-threaded. This means that your code runs sequentially, from top to bottom, until the end of the method. If you have validation occurring first, followed by other updates, your code will run to its end. Once it ends, if any records received an error (via addError), and allOrNone is "false", then those records are set aside, any work done is rolled back, and another attempt is made.

You can optimize your code by skipping any other updates when addError has already been used, because it'll either roll back the entire transaction anyways (allOrNone=true), or it'll cause the transaction to run twice (allOrNone=false). You should not perform any other work alongside validation rules as a matter of performance.

Realistically, that means you should write your trigger in a similar pattern:

if(TriggerHandlerClass.validatedRecords(Trigger.new)) {
    TriggerHandlerClass.processRecords(Trigger.new));
}

This will allow partial updates to run at an optimal speed.

5

they will execute linear in your trigger. So:

public void beforeUpdate( sObject oldObj, sObject newObj ){
    // will execute first
    method1( oldObj, newObj );
    // will execute second
    method2( oldObj, newObj );

}

If you use a trigger factory to call a trigger handler and execute bulk methods first, you will see the bulk execution pass your records into the appropriate method handler - in the order in which they are called as well.

  • One piece of advice- if using a trigger factory and you intend to call methods in the handler that may update a record, which in turn might cause an update to the same record, it will fail so try not to put yourself into an infinite loop as it will crash and the changes will not commit. Try to keep only the necessary updates within one cycle if possible of the trigger before and after and finally ( commit stage of execution ) in the trigger. – Ronnie Sep 14 '17 at 14:13
0

Having multiple triggers for same event is not recommended because Salesforce doesn't guarantee of order of execution of triggers if there are more than one. So in your case, validation may happen after insertion or before insertion.

Check out this page which talks about the order of execution.

  • 1
    There is only 1 trigger in the question, so the order of execution is not relevant to this. The threading for a single trigger is important as in @SFDCFox's answer. – Dave Humm Sep 14 '17 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.