PDI · Question #52
An Opportunity needs to have an amount rolled up from a custom object that is not in a master- detail relationship. How can this be achieved?
The correct answer is C. Write a trigger on the child object and use an aggregate function to sum the amount for all related. When a custom object is related to a parent via a lookup relationship (not master-detail), an Apex trigger on the child object is necessary to perform custom roll-up calculations, like summing an amount.
Question
An Opportunity needs to have an amount rolled up from a custom object that is not in a master- detail relationship. How can this be achieved?
Options
- AWrite a trigger on the child object and use a red-black tree sorting to sum the amount for all
- BWrite a Process Builder that links the custom object to the Opportunity.
- CWrite a trigger on the child object and use an aggregate function to sum the amount for all related
- DUse the Streaming API to create real-time roll-up summaries.
How the community answered
(23 responses)- A4% (1)
- B13% (3)
- C78% (18)
- D4% (1)
Why each option
When a custom object is related to a parent via a lookup relationship (not master-detail), an Apex trigger on the child object is necessary to perform custom roll-up calculations, like summing an amount.
Red-black trees are data structures for sorting, not directly related to summing amounts for roll-up functionality in Salesforce triggers.
Process Builder can update fields but is not designed for complex aggregate calculations across multiple related records like a roll-up summary; it's more for direct record updates or launching flows.
An Apex trigger on the child object can be used to execute logic when child records are created, updated, or deleted. Within the trigger, an aggregate SOQL query with SUM() can efficiently calculate the total amount from all related child records and update the parent Opportunity.
Streaming API is used for real-time notifications about data changes, not for performing server-side aggregate calculations and updating parent records.
Concept tested: Custom roll-up summaries with Apex triggers
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm
Topics
Community Discussion
No community discussion yet for this question.