Up until this point I was of the belief that roll-up summary operations didn't interfere with custom Apex and were calculated quietly in the backend. However, I am seeing otherwise.

Consider the following configuration:

  • Master object: Invoice
  • Detail object: Item
  • Trigger on before-update of Invoice -> inserts a child Item
  • Trigger on after-insert of Item -> clones Items, then explicitly updates them

Since ItemTrigger is running on after-insert it clones the records in order to be able to update them (this arrangement is part of a generalized complex business logic and is not changeable -i.e. cannot use before-insert).

When an Invoice record is edited I receive this log:

CODE_UNIT_STARTED  InvoiceTrigger on Invoice trigger event BeforeUpdate for [a0J36000003g4we]
DML_BEGIN          [24]|Op:Insert|Type:Item|Rows:1
CODE_UNIT_STARTED  ItemTrigger on Item trigger event AfterInsert for [a0L3600000B2WkR]
DML_BEGIN          [18]|Op:Update|Type:Item|Rows:1
DML_END            [18]|
CODE_UNIT_FINISHED ItemTrigger on Item trigger event AfterInsert for [a0L3600000B2WkR]
DML_END            [24]|
EXCEPTION_THROWN   [24]|System.DmlException: Insert failed. First exception on row 0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0J36000003g4we) is currently in trigger InvoiceTrigger, therefore it cannot recursively update itself: []
CODE_UNIT_FINISHED InvoiceTrigger on Invoice trigger event BeforeUpdate for [a0J36000003g4we]

Line 24 of InvoiceTrigger is this:

insert newItem;

Line 18 of ItemTrigger is this:

update item;

Assuming I didn't mix anything up during my debugging session I am surprised by the explicit database call. Is this at all expected?!

  • 1
    Rollup summaries cause the trigger on the record to fire. If a child update causes a rollup summary to be recalculated then the parent triggers will run. Just in case you were not aware – Eric Sep 30 '17 at 22:20
  • @Eric Totally not aware. That's very useful to know!! – Mossi Oct 1 '17 at 18:38

Yes, it's expected. See the Triggers and Order of Execution document. You need to anticipate this and plan accordingly, probably by using future methods.

  • Man, that Order of Execution document has grown! If I'm not mistaken it used to only mention triggers, workflows, validations, assignments, processes, etc. I don't remember seeing roll-ups in there. Also I'd been told roll-ups happen behind the scenes and cannot be triggered off of.. I guess I should check the basics more frequently! – Mossi Oct 1 '17 at 18:29
  • @Mossi It's been that big for a few releases now, but yeah, they've gotten more detailed than it was originally. – sfdcfox Oct 1 '17 at 18: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.