Preface: this post is part of the Bulkify Your Code series.
Maps are beloved because they’re the #1 tool for beating the most important Governor Limit!
A map is a search table that you build from scratch. You choose all the possible search terms, and for each one you add a search result. It’s like building your very own Google search!
Building and using map goes like this:
If you’re familiar with Excel, building a map is a lot like doing a VLOOKUP!
Let's make a map that gives us our friend's name when we search for their email: | |
Name | |
dvdkliu+sfdc99@gmail.com | David Liu |
sfdc99luver@gmail.com | Taylor Swift |
imsingle4udavid@gmail.com | Shakira |
davidgetalife@yahoo.com | Marissa Mayer |
Search using any term from this column | Your result will be the value from this column |
Now let’s build the same map in Apex:
// Step 1: The search term (email) data type will be a string // The search result (name) data type will also be a string Map<String, String> friendMap = new Map<String, String>();// Step 2: Add our friends as a search term/result pair friendMap.put('dvdkliu+sfdc99@gmail.com', 'David Liu'); friendMap.put('sfdc99luver@gmail.com', 'Taylor Swift');friendMap.put('imsingle4udavid@gmail.com', 'Shakira'); friendMap.put('davidgetalife@yahoo.com', 'Marissa Mayer');// Step 3: Search our map using the get() function String biggestFan = friendMap.get('sfdc99luver@gmail.com'); System.assertEquals('Taylor Swift', biggestFan); // TRUE
It may be difficult to tell now but I assure you that maps are the #1 destroyer of Governor Limits!
In fact, people often refer to maps as… the GOVERNATORs!
Next post: Bulkify your code by combining your SOQL queries!
Hi All,
I’m having a lot of confusion about when to use the map in Triggers. For ex:
There are 5 agents in the team with salaries of 10,20,50,30,10Lakhs & Their Salary got hiked by 20, 28, 60, 41, and 18 Lakhs.
Now how do I need to find out who got more in terms of % and the highest % person needs to get the Checkbox as Tick.
Request you to please help me.
hi David,
I want learn more Map concept so i have a requirement like this use Map instead of Nested ForLoops, give me a suggestion.
The concept is Avoiding Nested forloop and use Map please help me
Not bad the emails of your friends..!!! :’)
Hi David ,
Please can you see if this class is correct
@isTest
public class PopReselleroNGSMandCASE {
static testMethod void insertNewGSMHandshake() {
// Get a list of all active record types within the system associated to Accounts
List accRTypes = [Select Name, Id From RecordType where sObjectType=’Account’];
// Create a map between the Account record Type Name and Id for for easy retrieval
Map accountRecordTypes = new Map{};
for(RecordType accRT: accRTypes)
accountRecordTypes.put(accRT.Name,accRT.Id);
Account testReseller = new Account();
testReseller.Name=’Test of the Account1′;
testReseller.type=’Reseller’;
testReseller.RecordTypeId= accountRecordTypes.get(‘Reseller’);
testReseller.Shippingcountry=’United Kingdom’;
insert testReseller;
Account testAPA = new Account();
testAPA.Name=’Test of the Account1′;
testApa.APA_Name__c=’Forrest’;
testAPA.type=’APA’;
testAPA.RecordTypeId= accountRecordTypes.get(‘APA’);
testAPA.Reseller__c = testReseller.id ;
testAPA.Shippingcountry=’United Kingdom’;
insert testAPA;
Account testAPA1 = new Account();
testAPA1.Name=’Test of the Account1′;
testAPA1.APA_Name__c=’Global Heat Source’;
testAPA1.type=’APA’;
testAPA1.RecordTypeId= accountRecordTypes.get(‘APA’);
testAPA1.Reseller__c = testReseller.id ;
testAPA1.Shippingcountry=’United Kingdom’;
insert testAPA1;
Account testAPA2 = new Account();
testAPA2.Name=’Test of the Account1′;
testAPA2.APA_Name__c=’Keepmoat’;
testAPA2.type=’APA’;
testAPA2.RecordTypeId= accountRecordTypes.get(‘APA’);
testAPA2.Reseller__c = testReseller.id ;
testAPA2.Shippingcountry=’United Kingdom’;
insert testAPA2;
Account testHome = new Account();
testHome.Name=’Test of the Account1′;
testHome.type=’Home’;
testHome.RecordTypeId= accountRecordTypes.get(‘Home’);
testHome.Shippingcountry=’United Kingdom’;
testHome.Reseller__c = testReseller.id ;
testHome.APA__c= testAPA.id;
insert testHome;
Account testHome1 = new Account();
testHome1.Name=’Test of the Account1′;
testHome1.type=’Home’;
testHome1.RecordTypeId= accountRecordTypes.get(‘Home’);
testHome1.Shippingcountry=’United Kingdom’;
testHome1.Reseller__c = testReseller.id ;
testHome1.APA__c= testAPA1.id;
insert testHome1;
Account testHome2 = new Account();
testHome2.Name=’Test of the Account1′;
testHome2.type=’Home’;
testHome2.RecordTypeId= accountRecordTypes.get(‘Home’);
testHome2.Shippingcountry=’United Kingdom’;
testHome2.Reseller__c = testReseller.id ;
testHome2.APA__c= testAPA2.id;
insert testHome2;
GSM_Handshake__c testGSM_Handshake = new GSM_Handshake__c();
testGSM_Handshake.Contact_Name__c=’Test of the GSM_Handshake’;
testGSM_Handshake.Home_Account__c = testHome.id ;
insert testGSM_Handshake;
Case TestCase = new Case();
TestCase.accountid = testHome.id ;
TestCase.Reseller__c = testReseller.id;
TestCase.APA__c = testAPA.id ;
TestCase.status = ‘New’;
insert TestCase;
}
}
Great start so far, needs a little more work to be complete though!
1. It runs at least 75% of the code in the trigger you’re testing
2. Needs to use System.assertEquals() more
3. Make sure to test for things that shouldn’t work!
4. Bulk testing!!!
You’ll find example of all the above in the chapters on this site, especially chapter 7!
Hi David,
I love everything you are doing, you don’t understand how much this website has helped me become a stronger SFDC Admin and solid dev. I have a good understanding of List and Sets, do you by chance have any good material you can point me to that talks a little bit more about Maps?
Hi David –
I’ve learned so much from this site. But one thing I’m stuck on is Lists, Sets and Maps. I have this conceptual problem with them.
Here’s my confusion.
So you have Map like the above tutorial, “Map friendMap”.
Then add values to the Map like so: friendMap.put(‘dvdkliu+sfdc99@gmail.com’, ‘David Liu’);
This makes sense as the first string, the email, goes in the first String of the Map (Map.
And the second string, the name, goes in the second String of the Map (Map
But what about a Map and SELECT statment like so:
Map peopleToSpam = [SELECT Id, Email, Phone, Fax FROM Contact];
This select statement returns 4 fields, but there are only two places in the Map (ID, and Contact).
Can you help me wrap my head around this? How do I get 4 values into the 2 spots in the Map (ID, Contact)??
Thanks:)
The second spot is the Contact record itself! And the Contact is simply a collection of fields =)
Look at it this way:
You can have a basic Map that accepts a String and an Integer
Take it one level more, you have have a Map that accepts a String and a Contact (which might have 2 Integer fields and 2 String fields)
Take it another level, you can have a Map that accepts a String and a List of Contacts!
Or even another level, a Map that accepts a String and a List of Lists of Contacts!
Basically, the second part of the map can be anything you want it to be, whether it’s a single property (like an Integer) or a collection of properties (like any object).
Hope this helps!
David
OMG this, indeed, THIS helped me a lot!
Damn David, you rock!
Thanks for continuing to post relevant and approachable examples, David. Are these posts coming out on a schedule or just when you get around to them?
1 to 2 posts per week now that I am back from holidays =)
My New Year’s resolution is to write enough content that a reader will be able to get a mid-level job as a Salesforce developer after practicing all the tutorials. I interview a lot of Salesforce developers (at Google and at other companies) so I have a good grasp of what is needed!
Based on the current tutorials, that will be around chapter 10! Then I will pump out more videos, some tutorials on Visualforce, and perhaps a couple chapters on some very very advanced topics =)
I am most excited about the Hall of Fame challenge, make sure to check it out!
https://www.sfdc99.com/2013/11/25/hall-fame-challenge-coming-soon/
cool