13

Does managed package trigger code always execute before the org's user defined trigger code?

(i.e. Is the before insert logic of all managed packages that are installed in an org executed before the locally defined before insert logic?)

I have not been able to locate documentation specifically noting order of execution packaged vs. non-packaged code. In looking at the debug logs, it appears that triggers and workflows from the managed packages are always executed first but I would like to see documented evidence of this before accepting it as fact.

3
  • Great question. I always assumed it as fact as I always see it that way int he debug logs, but would love to see some proof that it actually is the case. Commented Sep 16, 2013 at 19:26
  • Can anyone provide a debug log sample with managed package code executing after user code?
    – Mark Pond
    Commented Sep 16, 2013 at 19:50
  • +1 for doing your validation in the after phase of your trigger, no code managed or otherwise can change the state of the record in this phase, so regardless of order you will can the final say if you don't like any changes made to your data. Commented Sep 17, 2013 at 15:30

4 Answers 4

9

AFAIK, it's irrelevant whether you have managed or unmanaged triggers (or both). The guidance I've heard is that any time you have multiple triggers on the same object in the same firing context (e.g. multiple before insert triggers on Lead), then the order of execution is undefined. It is best to have just one trigger in each context.

Edit: It is undefined. From the docs:

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.

8
  • 7
    If you have a managed package that has triggers, you obviously can't help but have multiple triggers on the same object if you have your own triggers on the same object. There's no way to avoid that. Commented Sep 16, 2013 at 19:40
  • I'm not saying it is avoidable. I'm saying in that kind of situation, the firing order is undefined. Commented Sep 16, 2013 at 19:41
  • I concur that the order in which things are executed is non-deterministic but it appears that there is always some order happening here... managed code is executed first, unmanaged second. Then within these two types of code, the order is not guaranteed.
    – Mark Pond
    Commented Sep 16, 2013 at 19:45
  • Indeed, but I'd still be uncomfortable assuming that managed triggers will always fire before unmanaged. Commented Sep 16, 2013 at 19:48
  • 1
    Even if the current implementation always does call managed package triggers first, I certainly wouldn't design based on that assumption. Since it isn't documented, SFDC could change it at any time.
    – kibitzer
    Commented Sep 16, 2013 at 23:08
2

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.

Please see below link

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

1
  • What I am looking to shore up is the detail within item #3 in the documentation (in my sample scenario above) with regard managed & unmanaged code.
    – Mark Pond
    Commented Sep 16, 2013 at 19:48
2

Order of Trigger execution (Managed and un-managed), there is no priority set for which package's trigger will be fired for particular DML OP.

The only order follows is before, after trigger. If there are two triggers on before insert on is managed and other one is user created, then we can not guarantee of which will fire first.

But in most cases it happened to see in debug logs that managed package trigger got fired first but it doesn't happens each time. I have seen in several debug logs where 6 to 7 triggers got fired including managed and unmanaged package triggers, they were all random, in which first is user defined, second from managed package and vice-versa. Also It is not happens that all managed trigger will fire in sequence and unmanaged by sequence, they are all random.

I never read a official documentation specifying the execution of triggers by managed package and unmanaged package. If someone, then please share.

When creating package it is best practice to create one trigger for same trigger type. And use classes to control the behaviour by checking some condition in trigger.

2

The order of execution of the triggers is based on "database" order, the order that the records get passed back from the database which is determined by storage location. So if the trigger is changed and the database can't fit the code in the same storage area, it could move it and thus the order will change. I have seen this happen.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .