Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam CRT-450 All Questions

View all questions & answers for the CRT-450 exam

Exam CRT-450 topic 1 question 91 discussion

Actual exam question from Salesforce's CRT-450
Question #: 91
Topic #: 1
[All CRT-450 Questions]

Which action can a developer take to reduce the execution time of the following code?

  • A. Put the Account loop inside the Contact loop.
  • B. Create an Apex helper class for SOQL.
  • C. Add a GROUP BY clause to the Contact SOQL.
  • D. Use a Map<Id List<Contact> for allContacts.
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
ABHI0O07
8 months ago
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 } } }
upvoted 1 times
...
edogaldo
1 year, 2 months ago
I seen none of these is correct...
upvoted 3 times
...
1vanTT
1 year, 6 months ago
Selected Answer: D
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
upvoted 1 times
edogaldo
1 year, 2 months ago
[SELECT Id ,(SELECT Id, AccountId FROM Contact) From Account] Wouldn't this return a list of accounts containing each a list of contacts?! I don't seem it would return a Map<Id,List<Contact>>, at most a Map<Id,Account> where each account would contain its own contacts..
upvoted 1 times
...
...
govosen887
1 year, 7 months ago
How does using the Map really help here? The problem is that we're retrieving all the accounts and all the contacts and then iterating through all the contacts for each account.
upvoted 1 times
mwwt2022
1 year, 1 month ago
bottleneck is nested loop. You should avoid nested loop whenever possible.
upvoted 1 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...