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
Arlene
4 days ago
Option A is definitely the way to go!
upvoted 0 times
...
Winifred
9 days ago
Not sure if A handles large data sets well.
upvoted 0 times
...
Jaime
14 days ago
A looks more efficient to me.
upvoted 0 times
...
Tawanna
20 days ago
Wait, can you really use opportunityList.AccountId like that in Option B?
upvoted 0 times
...
Sherell
25 days ago
I think Option B is simpler though.
upvoted 0 times
...
Gwenn
30 days ago
Option A is definitely the way to go!
upvoted 0 times
...
Alishia
1 month ago
I’m not confident, but I think Option B is trying to query directly from the Opportunity list, which might not be correct. I’d go with Option A based on what we practiced.
upvoted 0 times
...
Reita
1 month ago
I feel like I’ve seen a similar question before, and I think it was about querying related records. I’m a bit confused about Option B though; it seems like it might not work as intended.
upvoted 0 times
...
Ming
2 months ago
I'm not entirely sure, but I remember something about using a Set for IDs being a good practice. That makes me lean towards Option A too.
upvoted 0 times
...
Candra
2 months ago
I think Option A is the right choice because it uses a subquery to get all Contacts related to the Accounts, which seems more efficient.
upvoted 0 times
...

Save Cancel