Salesforce Certified Platform Developer II — Question 26
Part of a custom Lightning Component displays the total number of Opportunities in the org, which is in the millions. The Lightning Component uses an Apex
Controller to get the data it needs.
What is the optimal way for a developer to get the total number of Opportunities for the Lightning Component?
Answer options
- A. Apex Batch job that counts the number of Opportunity records
- B. SUM() SOQL aggregate query on the Opportunity object
- C. SOQL for loop that counts the number of Opportunities records
- D. COUNT() SOQL aggregate query on the Opportunity object
Correct answer: D
Explanation
The COUNT() SOQL aggregate query is optimal because it efficiently counts the number of records directly in the database, minimizing the data processed. In contrast, an Apex Batch job is not necessary for a simple count, SUM() is used for total values rather than counts, and a SOQL for loop is less efficient for counting as it retrieves all records first.