Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Salesforce Plat-Dev-301 Exam - Topic 2 Question 7 Discussion

Given a list of Opportunity records named opportunityList, which code snippet is best for querying all Contacts of the Opportunity's Account?A.JavaList contactList = new List ();Set accountIds = new Set ();for(Opportunity o : opportunityList){accountIds.add(o.AccountId);}for(Account a : [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIds]){contactList.addAll(a.Contacts);}B.20JavaList contactList = new List ();for ( Contact c : [SELECT Id FROM Contact WHERE AccountId IN :opportunityList.AccountId ]){contactList.add(c);}
A) Option A
B) Option B
C) Option C
D) Option D

Salesforce Plat-Dev-301 Exam - Topic 2 Question 7 Discussion

Actual exam question for Salesforce's Plat-Dev-301 exam
Question #: 7
Topic #: 2
[All Plat-Dev-301 Questions]

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

A.

Java

List contactList = new List ();

Set accountIds = new Set ();

for(Opportunity o : opportunityList){

accountIds.add(o.AccountId);

}

for(Account a : [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIds]){

contactList.addAll(a.Contacts);

}

B.

20

Java

List contactList = new List ();

for ( Contact c : [SELECT Id FROM Contact WHERE AccountId IN :opportunityList.AccountId ]){

contactList.add(c);

}

Show Suggested Answer Hide Answer
Suggested Answer: A

22

In Apex, 'bulkification' is the practice of ensuring code can handle multiple records efficiently without hitting governor limits. Snippet A demonstrates the correct bulkified approach for this requirement. It first iterates through the opportunityList to collect all unique AccountId values into a Set. Then, it performs a single SOQL query to retrieve all relevant Accounts and their child Contacts using a subquery (Inner Join). This ensures that the code only consumes one SOQL query regardless of how many opportunities are in the input list.

Snippet B is syntactically incorrect and will fail to compile. In Apex, you cannot use dot-notation (like opportunityList.AccountId) on a List collection to retrieve a set of IDs from its elements. To access the AccountId of records within a list, you must iterate through the list or use a map. Even if corrected to use a proper ID collection, Snippet A is often preferred when you need the relationship context between the Account and its Contacts. Most importantly, Snippet A correctly identifies the need to extract IDs into a separate collection before querying, which is a fundamental requirement for writing scalable Apex. It avoids the 'Query in a loop' anti-pattern and adheres to the platform's execution model.

==========


Contribute your Thoughts:

0/2000 characters

Currently there are no comments in this discussion, be the first to comment!


Save Cancel