How to use Map Collection in Salesforce?

on

|

views

and

comments

Table of Contents

Previous Session

If you have missed the previous session. You can get the complete information from the below link

How to use Collections in Salesforce Apex

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<String, String> countryCurrencies = new Map<String, String>();
				
			

Hands-On

				
					List<String> ABC = new List<String>();
#1 -

Map<String, Integer> accountMap = new Map<String, Integer>();
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<String, Integer> accountMap = new Map<String, Integer>{ 
    'Acme' => 10000, 'XYZ Corp'=> 500
};

Boolean exists = accountMap.containsKey('Acme');
// exists = True

accountMap.keySet(); // Returns the Set<Data Type of the Map Key>
accountMap.values(); // Returns the List<Data Type of Map Value>

Set<String> accountNameSet = accountMap.keySet();
List<Integer> revenueList = accountMap.values();

#3 - 
Map<String, Integer> accountMap

// Complex Map
				
			

Hands-On

				
					Map<String, String> accountMap = new Map<String, Sting>();
accountMap.put('Acme', '3454');
accountMap.put('XYZ Corp', '5000');
accountMap.put('ABC Company', '7500');

#1 -
Map<String, Object> objectMap = new Map<String, Object>();
Object
    String, Integer, Decimal, ApexClass, List, Map, Set  
    sObject
        Standard Object
        Custom Object
       
 Map<String, List<String> > countryStateMap = new Map<String, List<String> >();
				
			

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<String, String> countryMap = new Map<String, String>();
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<String> keys = countryMap.keySet();
List<String> values = countryMap.values();
System.debug(' keys \n '+ keys);
System.debug(' values \n '+ values);

// Country & their states (multiple values) - String, List<String> 
// Country - String
// Map< String, List<String> >
Map<String, List<String> > countryMap = new Map<String, List<String> >();
List<String> indiaStates = new List<String>{'UP', 'Delhi', 'MP', 'MH', 'HYD'};
countryMap.put('India', indiaStates);
// 'India' = String
// indiaStates - List<String>
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<String> 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<String> stateListTemp = countryMap.get('USA'); 
    stateListTemp.add('Florida');
    countryMap.put('USA', stateListTemp);
} else {
    List<String> stateListTemp = new List<String>(); 
    stateListTemp.add('Miami');
    countryMap.put('USA', stateListTemp);
}
System.debug(' countryMap \n '+ countryMap);
// Key - { 'India', 'USA' }


				
			

Assignment

  1. Create a List to Store Some of the Salesforce Certifications
  2. Create a Set to store the Salesforce Certifications related to Salesforce Developers
  3. 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/
    1. Key – Developer – Value – List of All the Certifications
    2. Key – Admin – Value – List of All the Certifications
    3. Key – Architect – Value – List of All the Certifications

Resources

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to start your AI Journey?

Table of Contents Introduction Are you tired of the same old world? Do you dream of painting landscapes with your code, composing symphonies with data, or...

The Secret Weapon: Prompt Engineering: 🪄

Table of Contents Introduction Crafting the perfect prompt is like whispering secret instructions to your AI muse. But there's no one-size-fits-all approach! Exploring different types of...

How to Singup for your own Production org?

Let's see how we can have our salesforce enterprise Salesforce Org for a 30-day Trial and use it as a Production environment. With the...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential