Maps In Salesforce

Introduction

The collection is a type of variable which stores numerous records temporarily. It’s a dynamic collection, meaning it increases or decreases the size dynamically and gets memory at runtime. We have 3 types of collections named Lists, Sets, and Maps.

What are Maps in Salesforce?

The map is one of the types of a collection which stores the records or data in a key-value pair (just like objects in JavaScript/JSON). It’s a key-value pair-based collection. It has no order and the key cannot be replicated i.e. key should be unique but values can be replicated.

Keys and values can be of any data type like collections, primitive data types, sObjects, etc.

It also helps developers to not hit Governor Limits.

How to declare a map in Salesforce?

We can declare a map using the Map keyword followed by the data types of keys and values within <> characters.

For example:

Map<String, Integer> employees = new Map<String, Integer>();

 

We can initialize the map or populate the key-value pair with the values at the time of declaration by using curly braces ( {} ). We specify the key first and then the value.

Example:

Map<String, String> students = new Map<String, String>
{ ‘Student1’ => ‘stu1’, ‘Student2’ => ‘stud2’ };

 

We have multiple methods given by Salesforce in Apex

1. keyset() – It returns all the keys of Map.

2. values() – It returns all the values of the Map

3. put(key, value) – To insert the keys and values in Map.

Important Considerations in Map

1. A key in the map can hold a null value.

2. If the data type of key in Map is String, then keys would be case-sensitive.

3. Apex uses a Hash structure for all maps.

4. Adding a map entry with an existing key overrides the existing key with a new one.

5. Keys should be unique, but values can be repeated.

6. Keys and values in the map can contain any collection and nested collections.

 

How to use Maps to navigate across Lists?

trigger ConAccT on Contact (before insert) {
Set<Id> accIds = new Set<Id>();
for(Contact c : Trigger.new){
accIds.add(c.AccountId);
}
List<Account> accList = [SELECT Id, Name, Phone FROM Account WHERE Id IN :accIds];
Map<Id, Account> accMap = new Map<Id, Account>();
for(Account acc : accList){
accMap.put(acc.Id, acc);
}


if(accMap.size() > 0){
for(Contact cc : Trigger.new){
if(cc.AccountId != null && accMap.containsKey(cc.AccountId)){
cc.OtherPhone = accMap.get(cc.AccountId).Phone;
}


}
}
}

 

 

If we see below part of the code:

List<Account> accList = [SELECT Id, Name, Phone FROM Account WHERE Id IN :accIds];
Map<Id, Account> accMap = new Map<Id, Account>();
for(Account acc : accList){
accMap.put(acc.Id, acc);
}

 

We are using the query to retrieve those Account records with fields Name, Id, Phone whose Id is equal to the Contact Account Id records which we are inserting. And then, for loop is iterating over the accList to add account Id as key and complete Account record as a value in Map using put method.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.