Preparing Practice Exam…
Please wait a moment while the system generates the exam questions, answers, and study session.
Please wait a moment while the system generates the exam questions, answers, and study session.
Salesforce Certified Platform Developer I (PDI) · Salesforce
Select one answer.
Written by other learners. Comments can disagree with the marked answer — check the official documentation when in doubt.
List<Account> al = [SELECT Id FROM Account]; List<Contact> cl = [SELECT Id, AccountId FROM Contact]; // Map to associate each Account Id with its corresponding Contacts Map<Id, List<Contact>> accountContactMap = new Map<Id, List<Contact>>(); // Populate the map for (Contact c : cl) { if (!accountContactMap.containsKey(c.AccountId)) { accountContactMap.put(c.AccountId, new List<Contact>()); } accountContactMap.get(c.AccountId).add(c); } // Iterate through Accounts and their Contacts using the map for (Account a : al) { if (accountContactMap.containsKey(a.Id)) { List<Contact> contacts = accountContactMap.get(a.Id); for (Contact c : contacts) { // Perform work here with Account a and Contact c } } }
I seen none of these is correct...
The answer is D. You should retrieve the child contact data along with the parent: [SELECT Id ,(SELECT Id, AccountId FROM Contact) From Account]; This will return a nested list of objects (answer D) Ref: https://medium.com/code-85/how-to-query-parent-and-child-relationships-in-soql-13445ace3c7f