0

In one of my Custom object, there are 4 triggers.( I know, ideally there shuold be one but there are present for long time and we might need some time to correct and put everything in one code)

Coming to question:each trigger executing in multiple contexts. I mean,some of the code in 'each' of them fire in 'before' context as well as some in 'after'context.

I know, there is no guarantee trigger will be executed in particular order. But does that mean for 'each' trigger, both 'before' and 'after' context are executed and then execution of 2nd(another) trigger will start? Or is that we can say, code in all triggers in 'before context' will be executed first( probably in random order) and then code in all triggers in 'after context' will be executed?

For e.g there is one trigger A , a part of code execute in 'before' context' and update field X as true another trigger B fires only if X == true but in 'after' context and create record in another object.

Is there guarantee that above won't create any problem ? i.e before, 2nd trigger(B) check value of X in 'after context', trigger A already would have updated its value to true and record will be safely created in another object?

Can we say before portion of each trigger will be executed first ?

1

It really depends how your triggers are coded.

Consider the following code:

trigger AccountTrigger on Account (before update, after update) {

    for (Account a: Trigger.new) {

        // do something that is executed in both before and after update

        if (Trigger.isBefore) {
            a.Field_2__c = true;
        }

        if (Trigger.isAfter) {
            if (a.Field_2__c) {
                // do something else
            }   
        }
    }
}

When an Account object is updated, and the before triggers are run, the entire trigger will be executed. As the value of Trigger.isAfter is false at this point, the code inside the last if block will be skipped.

When the after triggers are run, the entire trigger will again be executed. As the value of Trigger.isBefore will be false now, that block is skipped, but everything else is run. Any values that we set in the Before trigger will still be set when the code in the After triggers are run, so in this example, a.Field_2__c will always be true regardless of the initial value of the field.

This example is self-contained, but it's true regardless of which trigger updated which field. Provided the field updates you want to rely on were executed as part of a Before trigger, the values will be there when your After trigger executes

For more information, I highly recommend the following resources for Salesforce order of execution, and Trigger context variables that can help you ensure only the code you want to be executed at a certain time actually is:

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.