Search code examples
triggersrelational-databasesalesforceapex-code

MDR clarification


Setup:

I have a MDR relationship as shown below:

Event --< Attendee >-- Contact

There is a trigger on EVENT that fires before insert and before update that calculaes the number of attendees (by searching for the number of occurances of the Event's ID in the attendees object.

Question:

When I'm adding a record to the ATTENDEE why is my trigger being fired on the EVENT object?!

Thanks!


Solution

  • When a change to a detail record (in a Master-Detail relationship) causes a roll-up summary on the Master record to change, it may cause Trigger(s) on the Master record to execute.


    The Roll Up Summary Field Technology Overview knowledge article, in Salesforce help, explains it well:

    Incremental Updates
    When a summarized record changes we determine whether that change would lead to a change in the RSF value. If so, we calculate a delta value, which is then applied to the RSF in the summarizing record in the same transaction. We perform this operation in memory. No additional database call is required. Updates are only performed for the row(s) with RSF that actually changed. If the summarizing row itself is summarized the process continues to the grandparent. All validations, triggers, workflow, etc, for the summarizing row are necessarily executed when they are changed. All Incremental updates are performed to 38 digits of precision.

    Also, see page 79 of the the Spring '09 release notes for more detail.

    Workflow Rule and Roll-Up Summary Field Evaluations
    The Spring '09 Workflow Rule and Roll-Up Summary Field Evaluations update affects the way Salesforce evaluates workflow rules and roll-up summary fields on objects with Apex triggers. The update improves the accuracy of your data and prevents the reevaluation of workflow rules in the event of a recursion. A recursion is a situation in which a part of your custom logic (such as a roll-up summary field or Apex trigger) causes Salesforce to execute the logic twice when saving a record, often resulting in undesirable behavior. For more information, see "What is the Spring '09 Workflow Rule and Roll-Up Summary Field Evaluations Update?" in the Salesforce online help.