PDI practice question 23 of 30

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?

Full set: 201 questions
Answer options
A: Write a trigger on the Opportunity object and use tree sorting to sum the amount for all related child objects under the Opportunity.
B: Use the Streaming API to create real-time roll-up summaries.
C: Write a trigger on the child object and use an aggregate function to sum the amount for all related child objects under the Opportunity. Correct answer
D: Use the Metadata API to create real-time roll-up summaries.
Correct Answer:
C. Write a trigger on the child object and use an aggregate function to sum the amount for all related child objects under the Opportunity.
Explanation

Standard roll-up summary fields are only available for objects in a master-detail relationship. When a roll-up is required for objects linked by a lookup relationship, the standard programmatic solution is to use an Apex trigger. The trigger must be placed on the child object because its records' creation, modification, or deletion events are what initiate the need for recalculation on the parent. Within the trigger, an aggregate SOQL query using a function like SUM() is used to efficiently calculate the new total from all related child records, which is then used to update a field on the parent Opportunity.

Why Incorrect

A. A trigger on the parent (Opportunity) object will not execute when a child record is created, updated, or deleted, making it unsuitable for this roll-up logic.
B. The Streaming API is designed for publishing and subscribing to event notifications for real-time data synchronization with external systems, not for server-side data aggregation.
D. The Metadata API is used to manage the setup and configuration (metadata) of a Salesforce org, such as creating objects or fields, not for manipulating record data. ---

References

1. Apex Developer Guide, "Triggers": The guide states, "A trigger is Apex code that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted." This confirms a trigger on the child object is the correct mechanism to respond to the DML events that require a roll-up recalculation.

2. Apex Developer Guide, "SOQL and SOSL Queries", Section: "Using Aggregate Functions": This section documents the use of aggregate functions like SUM() in SOQL queries. It states, "Use an aggregate function in a GROUP BY clause in a SOQL query to roll up and summarize data." This is the precise technique used within the trigger to calculate the sum from child records.

3. Trailhead, "Apex Triggers" Module, Unit: "Bulk Apex Triggers": This unit provides a canonical example of a trigger that updates a parent record based on changes to its children. The example logic involves collecting parent IDs from child records in Trigger.new and Trigger.old, running a single aggregate SOQL query to get updated counts, and then updating the parent records. This directly illustrates the pattern described in the correct answer.

Scroll to Top