Plat-Dev-201 practice question 27 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: 204 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 only function across master-detail relationships. To replicate this functionality for a lookup relationship, an Apex trigger is the appropriate solution. The trigger should be placed on the child object. When a child record is created, updated, deleted, or undeleted, the trigger fires. Inside the trigger, an aggregate SOQL query (e.g., SELECT SUM(Amountc) FROM Childc WHERE Opportunityc = :oppId) can calculate the new total for the parent Opportunity and update a custom field on it.

Why Incorrect

A. A trigger on the parent (Opportunity) object will not fire when a child record is modified, so it cannot react to changes that require a roll-up recalculation.
B. The Streaming API is a notification service for pushing data changes to external clients; it is not used for server-side data aggregation.
D. The Metadata API is used for deploying and retrieving an org's customizations (metadata), not for performing data-level calculations.

References

Salesforce Apex Developer Guide, "Triggers". "Triggers can be used to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions."

Salesforce Apex Developer Guide, "Working with SOQL Aggregate Functions". "Use aggregate functions in a GROUP BY clause in SOQL queries to generate reports for analysis... Common functions include SUM() and COUNT()." This is the mechanism used within the trigger.

Trailhead by Salesforce, "Apex Triggers" module, "Trigger Apex". This module explains how triggers respond to DML events, which is the foundation for this roll-up pattern.

Scroll to Top