In this blog, we will be reading about Map Methods in Salesforce so that your organization can derive optimum value out of your alesforce-powered business.
Before we continue any further, it is important for us to gain a clear and complete insight into Maps in Salesforce so that we are on the same level of understanding.
A Map can be referred to as a collection of key-value pairs where each unique key maps to a single value. It is worthwhile to note here that keys can be of any primitive data type while values would only be a primitive, collection type, sObject, or an Apex object. It is also important to remember here that each and every element in the map contains key and value. Keys should be unique and value can be duplicated. They support dynamic memory allocation.
Let us now find out about how to create a map, map methods, and how to correct invalid type error iterating over a map.
Syntax
Creating a Map:
Map<Key,Value> mapName = new Map<Key,Value>();
In salesforce, we have standard object Account, If we want to map id with Account Name, it is represented as,
Map<Id, String> accIdVsAccNamemap = new Map<Id, String>();
Map Methods:
clear()
Removes all of the key-value mappings from the map.
clear()
Makes a duplicate copy of the map.
containsKey(key)
Returns true if the map contains a mapping for the specified key.
deepClone()
Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values.
equals(map2)
Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false.
get(key)
Returns the value to which the specified key is mapped, or null if the map contains no value for this key.
getSObjectType()
Returns the token of the sObject type that makes up the map values.
hashCode()
Returns the hashcode corresponding to this map.
isEmpty()
Returns true if the map has zero key-value pairs.
keySet()
Returns a set that contains all of the keys in the map. If you hire a maid for home cleaning in Georgia, she will explain to you how to clean the inside of a microwave.
put(key, value)
Associates the specified value with the specified key in the map.
putAll(fromMap)
Copies all of the mappings from the specified map to the original map.
putAll(sobjectArray)
Adds the list of sObject records to a map declared as Map<ID, sObject> or Map<String, sObject>.
remove(key)
Removes the mapping for the specified key from the map, if present, and returns the corresponding value. Visit blairsupplyusa.com.
size()
Returns the number of key-value pairs in the map.
values()
Returns a list that contains all the values in the map.
Nested Map in Salesforce:
There is a nested limit of 7 collections. If you attempt to go beyond this, you’ll get an error:
- Nested type exceeds maximum level: 8
Example for nested map:
Code Snippet
Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,String>>>>>>>> abc_Map = new Map<String, Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,String>>>>>>>>();
Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,String>>>>>>> temp_1 = new Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,String>>>>>>>();
Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,String>>>>>> temp_2 = new Map<String,Map<String,Map<String,Map<String,Map<String,Map<String,String>>>>>>();
Map<String,Map<String,Map<String,Map<String,Map<String,String>>>>> temp_3 = new Map<String,Map<String,Map<String,Map<String,Map<String,String>>>>>();
Map<String,Map<String,Map<String,Map<String,String>>>> temp_4 = new Map<String,Map<String,Map<String,Map<String,String>>>>();
Map<String,Map<String,Map<String,String>>> temp_5 = new Map<String,Map<String,Map<String,String>>>();
Map<String,Map<String,String>> temp_6 = new Map<String,Map<String,String>>();
Map<String, String> temp_7 = new Map<String,String>();
temp_7.put(‘7’ , ‘Nested’);
temp_6.put(‘6’ , temp_7);
temp_5.put(‘5’ , temp_6);
temp_4.put(‘4’ , temp_5);
temp_3.put(‘3’ , temp_4);
temp_2.put(‘2’ , temp_3);
temp_1.put(‘1’ , temp_2);
abc_Map.put(‘0’ , temp_1);
System.debug(‘abc_Map : ‘ + (abc_Map.get(‘0’).get(‘1’)).get(‘2’).get(‘3’).get(‘4’).get(‘5’).get(‘6’).get(‘7’));
Conclusion
If you need any assistance with your Salesforce requirements, it is best to trust only a reputed provider of Salesforce Implementation services and Salesforce Consulting Services by choosing Cloud Analogy, the world’s number 1 Salesforce Development Company. Access the best Salesforce Admin & Developer Support! Explore our amazingly affordable Salesforce Development Packages and Salesforce Consulting Packages now.