Salesforce Certified Platform Developer II — Question 197
A developer has a requirement to query three fields (Id, Name, Type) from an Account and First and Last names for all Contacts associated with the Account.
Which option is the preferred optimized method to achieve this for the Account named ‘Ozone Electronics’?
Answer options
- A. Account a = [SELECT ID, Name, Type FROM Account WHERE name=‘Ozone Electronics’]; list lContacts = [SELECT firstname, lastname FROM Contact WHERE accounted=:a.ID];
- B. Account a = [SELECT ID, Name, Type, (SELECT FirstName, LastName FROM Contacts) FROM Account WHERE name=‘Ozone Electronics’ LIMIT 1];
- C. list lContacts = new list(); for (Contact c :[SELECT firstname, lastname, Account.Name, Account.ID, Account.Type FROM Contact WHERE Account.Name=‘ozone electronics’]) ( lContacts.add(c); )
- D. list lAccounts = [SELECT ID, Name, Type FROM Account JOIN (SELECT ID, firstname, lastname FROM Contact WHERE contact.account.name = ‘ozone electronics’)];
Correct answer: A
Explanation
Option A is the preferred method as it executes two separate queries efficiently, first fetching the Account and then the related Contacts using the Account ID. Option B is less optimal because it attempts to retrieve the Contacts in a subquery, which can be less efficient than handling them in separate queries. Option C unnecessarily retrieves additional fields and uses a loop, making it less efficient. Option D is incorrect as it uses a JOIN which is not a supported operation in SOQL for this context.