A great blog on Salesforce Maps by John De Santiago

Maps are powerful for managing data in Salesforce Apex. Recently I’ve been learning them and struggling but I stumbled onto this blog post which is awesome sauce! Enjoy. THIS guy just changed my life.

Imagine you had to wash 100 T-shirts. Would you wash them one by one—one per load of laundry, or would you group them in batches for just a few loads? The benefit of coding in the cloud is that you learn how to write more efficient code and waste fewer resources.

Read: Running Apex Code within Governor Execution Limits.

Think of maps like a list, but with separated record ID (Keyset) and records (Values). Futher, the Values side can be another List. (Mind Blown!) Rather than query for data many times, you query for data once into a Set, and use the Set tooling to access that data in your Apex.

What made me write this post. Well, it’s taken 2 weeks of deliberate effort to understand ALL of the features that come with Maps. They require constant use to learn them. But honestly, learning maps is no harder than learning to separate egg yolks from the whites.

Big win for today’s post. No more iterating over lists! Just create a map directly from the list.

“Once you have instantiated a map, you can add values to the map simply by using the put() method. However, if you happen to have a list of sObjects you can just pass that list in the constructor like so:

Map<Id, Account> accountsById = new Map<Id, Account>(listOfAccounts);


John’s Blog Post



Salesforce Developer Guide on Maps


Play with a map in Developer Console?

Go to Developer Console | Debug | Open Execute Anonymous Window

List<account> accList=new List<account>();

accList=[select id,Name from account Limit 20];//Querying the existing records into the list
Map<Id,Account>  accMap= new Map<Id,Account>();

for(Account acc:accList) {

 accMap.put(acc.id,acc); // Adding the accountID as key and Account record as value to the accMap


System.debug(‘Accounts stored in the accMap as Key-value pair   :’+accMap);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s