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 questionsStandard 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.
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. ---
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.
Keep practicing
Take the free PDI practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other PDI practice questions
- Question 1What can be used to override the Account's standard Edit button for Lightning Experience?
- Question 4Universal Containers (UC) wants to lower its shipping cost while making the shipping proc…
- Question 5A developer has an integer variable called maxAttempts. The developer needs to ensure tha…
- Question 7Universal Containers wants to back up all of the data and attachments in its Salesforce o…
- Question 8Universal Containers hires a developer to build a custom search page to help users find t…
- Question 9As part of new feature development, a developer is asked to build a responsive applicatio…
