Use of Maps Data Collection in Apex (Part -1)

  • Map data collection in Apex Salesforce is used to store key-value pairs
  • The key and value can be of data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
  • Maps are useful for working with large data sets, storing multiple values for a single key, and performing operations on groups of similar data
  • Can be used to store information about a group of related records. For example, the key is the account name and the value is the account’s ID.
  • Can use map methods like keySet, values, put, and get to perform operations on the data stored in the map.
  • Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct.
  • Uniqueness of map keys of user-defined types is determined by the equals and hashCode methods, which you provide in your classes.
  • Uniqueness of keys of all other non-primitive types, such as SObject keys, is determined by comparing the objects’ field values.

Sample Scenarios and code to create and use maps :

1. If direct map of Id and SObject is required –

Sometimes, we need a map of Id and SObject of same type. We can use the map constructor for this scenario. It returns an Id field as the key. This is a very useful way to populate a map without using a loop. Below is an example of creating a Map in Apex that uses an ID as the key and a SObject as the value:

//Creating a new map called accountsMap which uses an Id as the key and an Account SObject as the value
Map<Id,Account> accountsMap = new Map<Id,Account>([SELECT Id, Name FROM Account]);

//Retrieving the Account Name by its Id
String accName = accountsMap.get(accId);

In this example, we are creating a new Map called accountsMap. This Map uses an ID as the key. It uses an Account Sobject as the value.

The ([SELECT Id, Name FROM Account]) is using a so-called “map constructor.” It creates a new map. This map is populated with the results of the query SELECT Id, Name FROM Account. The key of each entry will be the Id field and the value will be the Account object record values.

Note – You can only create this map to store the Id field of an SObject as a key. No other fields can be used.

2. If map of any data type as key and any data type as values Map <String/Id/etc.,Sobject/String/Decimal/etc.> is required

Below is an example in Apex. It can be used to create a map of case statuses and their corresponding case numbers. In this map, the key is the case numbers and the value is the case status.

/* Declaring a new map called openCases which uses an CaseNumber as the key 
 * and Case record as the value*/
Map<String, Case> openCases = new Map<String, Case>();
 
// Query all Case records and iterate through each one
for (Case c : [SELECT Id, CaseNumber, Status, Priority FROM Case WHERE IsClosed = false]) {
    // Check if the case's status is open
    if(c.Status == 'Open'){
        /*Add the open case to the map as key value pair
		where key is CaseNumber and value is case SObject*/
        openCases.put(c.CaseNumber, c);
    }    
}
 
//Retrieving the Case Status by its Case Number
String caseStatus = openCases.get(case.CaseNumber);

Similarly, the following is an example in Apex that creates a map. This map is used to look up the Amount of all opportunities related to a specific opportunity. The key is the Opportunity’s ID. The value is the Amount.

/* Declaring a new map called oppAmountByOppId which uses an Id as the key 
 * and Opportunity Amount as the value*/
Map<Id, Decimal> oppAmountByOppId = new Map<Id, Decimal>();

// Query all Opportunity records and iterate through each one
for (Opportunity opp : [SELECT Id, Amount FROM Opportunity]) {
    //Check if the opportunity's Amount is not null
    if(opp.Amount != null)
    {
        /*Add the opportunity to the map as a new key-value pair, 
		where the key is the opportunity's Id and the value is the opportunity Amount*/
        oppAmountByOppId.put(opp.Id, opp.Amount);
    }
}

//Retrieving the Amount by its Id
Decimal oppAmount = oppAmountByOppId.get(oppId);

Leave a comment