While studying for my Platform Developer 1 exam, I came across this line. Could someone please explain how this is the case?

Question: How can a developer avoid exceeding governor limits when using an Apex Trigger?

The two correct answers provided are:

A: By performing DML transaction s on lists of sObjects. (This one I understand)

B: By using Maps to hold data from query results.

"B" has me stumped:

  1. How does using the Map data type help to avoid exceeding governor limits.
  2. May I please see a code example.
  • 3
    I think this is a pretty reasonable (and answerable) question, but SFSE tends to delete any questions that appear to contain exam content to ensure that we do not participate in the disclosure or discussion of proprietary/confidential material. Could you perhaps rephrase your post to remove what looks like exam content and focus on your conceptual question? This is not one of the publicly posted example questions from the Platform Developer I exam guide. – David Reed Nov 28 '18 at 3:12

In its simplest form, most developers have seen the most obvious allusion so many times they barely notice it any more.

There is a very common pattern where you see novice developers make a mistake like the following:

for (Opportunity record : trigger.new)
    Account parent = [SELECT Name, Etc__c FROM Account WHERE Id = :record.AccountId];
    // do stuff

There is also a very common pattern for avoiding this wasteful governor usage:

Set<Id> accountIds = new Set<Id>();
for (Opportunity record : trigger.new)

Map<Id, Account> parents = new Map<Id, Account>([
    SELECT Name, Etc__c FROM Account WHERE Id IN :accountIds

for (Opportunity record : trigger.new)
    Account parent = parents.get(record.AccountId);
    // no additional queries consumed
| improve this answer | |

First approach - First make query, store result in list, iterate on list and then put in map. Like below -

Map<String, Account> mapOfAccIdAndRecord = new Map<String, Account>();
List<Account> accList = [SELECT Id, Name, Phone FROM Account];
for(Account acc : accList){
  mapOfAccIdAndRecord.put(acc.Id, acc);

Second approach (For B point)- direct assign query result to map. This will help in basically to reduce the chances of Apex CPU time limit error.

Map<String, Account> mapOfAccIdAndRecord = new Map<String, Account>([SELECT Id, Name, Phone FROM Account]);
| improve this answer | |
  • 1
    This really does not address the point the asker is looking for - how this structure helps safeguard governor limits. – David Reed Nov 28 '18 at 12:23
  • This is basically to reduce the chances of Apex CPU time limit exceed error. @DavidReed – Radhe Radhe Nov 28 '18 at 15:30
  • 1
    Could you illustrate and explain that in your answer? – David Reed Nov 28 '18 at 15:33
  • 1
    I don't mean to belabor, but creating a Map does not per se save CPU time. (If you don't need the Map as such, it wastes CPU time). I thought you might be talking about using them to avoid naive iteration-based search algorithms, but I still don't see how this is an answer. I'll leave it here. – David Reed Nov 28 '18 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.