Salesforce Certified Platform Developer II — Question 148

Given a list of Opportunity records named opportunityList, which code snippet is best for querying all Contacts of the Opportunity's Account?

Answer options

Correct answer: C

Explanation

Option C is correct because it efficiently collects all Account Ids from the Opportunity records, then queries the Accounts in a single SOQL statement to retrieve their related Contacts. Option A incorrectly queries each Opportunity's Account individually, leading to inefficient SOQL usage. Option B focuses on Contact Ids instead of Account Contacts, missing the requirement. Option D incorrectly tries to reference AccountIds directly from the list instead of aggregating them first.