2

I am facing a rather bizarre situation. I am running a test class, wherein a record is inserted. On checking the debug logs i see that all trigger events are getting fired for this record. That is before insert, after insert, before update, and after update.

I have marked the trigger events in the snapshot with red (deliberately hidden the object names with black color). I fail to understand how can before update come after after insert trigger for an insert operation.

There is a workflow in between after insert trigger and before update. Workflow coming after after insert is understandable, but why is update also happening simultaneously?

Debug log

2
  • If your workflow has a field update action, and that workflow rule meets its criteria, then the field update will cause any update trigger(s) to be run (just like it would if a human user performed that update themselves).
    – Derek F
    Commented Apr 6, 2017 at 19:11
  • 1
    Thanks for the quick reply. But just to dwell a little further on this, could you also explain why does it(workflow action) count as an update. I mean when the workflow is invoked on account of insert operation.
    – starhunter
    Commented Apr 6, 2017 at 19:21

1 Answer 1

5

Looking at the screenshot of the log that you've provided, you do have a workflow rule that is being run after your after insert trigger finishes.

Your workflow rule has a field update action, and the log shows that this field update action is being run.

The reason why this causes your trigger to be fired again (after update) is explained by looking at the documentation on Triggers and Order of Execution (OoE for short)

The relevant section of the OoE is this (emphasis mine):

  1. Executes all after triggers.
  2. Executes assignment rules.
  3. Executes auto-response rules.
  4. Executes workflow rules.
  5. If there are workflow field updates, updates the record again.
  6. If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules, duplicate rules, and escalation rules are not run again.

For step 12 of the OoE, it doesn't care if you started the whole process from an insert (or a delete/undelete). If the object that the field update affected has any trigger(s) that run before update or after update, those trigger(s) will be run after the workflow field update finishes doing its thing.

You must log in to answer this question.

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