Table of Contents
Previous Session
If you have missed the previous session. You can get the complete information from the below link
Introduction
In Salesforce, a Map is a collection type that stores data in key-value pairs, where each key is unique and can be of any data type, and each value can also be of any data type. Maps are useful when you want to store data in an organized way and retrieve it based on a specific key.
- With the help of Maps, we can store the value using Key-value pairs (KV)
- Map is the combination of Set & List where Key can not contain the Duplicate Records & Value can contain a duplicate value.
- Map is very powerful while using Apex Code in Salesforce
Structure of Map
Map countryCurrencies = new Map();
Hands-On
List ABC = new List();
#1 -
Map accountMap = new Map();
accountMap.put('Acme', 10000);
accountMap.put('XYZ Corp', 5000);
accountMap.put('ABC Company', 7500);
// Retrieve the value for a specific key
Integer acmeValue = accountMap.get('Acme');
System.debug(acmeValue); // Outputs 10000
#2 -
Map accountMap = new Map{
'Acme' => 10000, 'XYZ Corp'=> 500
};
Boolean exists = accountMap.containsKey('Acme');
// exists = True
accountMap.keySet(); // Returns the Set
accountMap.values(); // Returns the List
Set accountNameSet = accountMap.keySet();
List revenueList = accountMap.values();
#3 -
Map accountMap
// Complex Map
Hands-On
Map accountMap = new Map();
accountMap.put('Acme', '3454');
accountMap.put('XYZ Corp', '5000');
accountMap.put('ABC Company', '7500');
#1 -
Map objectMap = new Map();
Object
String, Integer, Decimal, ApexClass, List, Map, Set
sObject
Standard Object
Custom Object
Map > countryStateMap = new Map >();
Methods of Map in Salesforce
There are numerous methods of Map in Salesforce, however, there are some that are being used widely by the developers.
- put(key, value) – Add a value in Map
- get(key) – get a value for the Key //String noOfEmployees = accountMap.get(‘Acme’);
- containsKey(key) – accountMap.containsKey(‘Amit’); //True/False
- keySet() – Set<Key Data Type> // Set<String> accountNameSet = accountMap.keySet();
- values() – List<Value Data Type>
- List<String> accountEmployees = accountMap.values();
- List<Object> accountEmployees = objectMap.values();
- List< List<String> > statesList = countryStateMap.values();
- clear()
- size()
- isEmpty()
- remove( key )
- putAll(anotherMap)
Hands-On with Methods
// Country & their Currencies
Map countryMap = new Map();
countryMap.put('India', 'INR');
countryMap.put('Japan', 'YEN');
if( countryMap.containsKey('USA') == false ){
countryMap.put('USA', 'US Dollers');
}else{
System.debug('Key already exisit in the Map');
}
System.debug(' countryMap '+ countryMap);
String japanCurrency = countryMap.get('Japan'); // null
System.debug(' japanCurrency \n '+ japanCurrency);
Set keys = countryMap.keySet();
List values = countryMap.values();
System.debug(' keys \n '+ keys);
System.debug(' values \n '+ values);
// Country & their states (multiple values) - String, List
// Country - String
// Map< String, List >
Map > countryMap = new Map >();
List indiaStates = new List{'UP', 'Delhi', 'MP', 'MH', 'HYD'};
countryMap.put('India', indiaStates);
// 'India' = String
// indiaStates - List
System.debug(' countryMap \n '+ countryMap);
// containsKey, get
// UK
// Step 1 - Check if Country exists as a Map in Key
// Step 2 - get the value from the map for that Country
// Step 3 - Add the new value into the value from step 2
// Step 4 - update the map
// Step 5 - print the values
if( countryMap.containsKey('India') ){ // Step 1
List stateListTemp = countryMap.get('India'); // Step 2
// {'UP', 'Delhi', 'MP', 'MH', 'HYD'}
stateListTemp.add('UK'); // Step 3
// {'UP', 'Delhi', 'MP', 'MH', 'HYD', 'UK' }
countryMap.put('India', stateListTemp); // Step 4
}
System.debug(' stateListTemp \n '+ countryMap);
if( countryMap.containsKey('USA') ){ // False
List stateListTemp = countryMap.get('USA');
stateListTemp.add('Florida');
countryMap.put('USA', stateListTemp);
} else {
List stateListTemp = new List();
stateListTemp.add('Miami');
countryMap.put('USA', stateListTemp);
}
System.debug(' countryMap \n '+ countryMap);
// Key - { 'India', 'USA' }
Assignment
- Create a List to Store Some of the Salesforce Certifications
- Create a Set to store the Salesforce Certifications related to Salesforce Developers
- Create a Map to store the Salesforce Certifications in the following Fashion – Note you can get the list of all the certifications by Job Role from here – https://trailhead.salesforce.com/en/credentials/administratoroverview/
- Key – Developer – Value – List of All the Certifications
- Key – Admin – Value – List of All the Certifications
- Key – Architect – Value – List of All the Certifications
Resources
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_set.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_map.htm