Salesforce

Salesforce Apex – Map

Salesforce Apex map is a data structure that is mainly used in trigger scenarios and helps to load more data at a time into the Salesforce database like a list. But it stores and organizes the data in the {key:value} pair format. We will discuss the map collection in Apex programming language and its methods. Here, we will use the Account standard object in Salesforce for all examples. Let’s quickly dive into this tutorial.

Map

Map takes the {key:value} pair data as input and stores it into the Salesforce standard or custom objects. It can take the sObject as the key or value.

Map Creation

By specifying the data types of the key and value along with the object name, the map can be created. Here, a new keyword is used to create it. It can be optional to pass the elements during creation.

Empty Map Syntax:

Map<key_datatype, value_datatype> map_obj = new Map<key_datatype, value_datatype>():

Generic Syntax:

Map<key_datatype, value_datatype> map_obj = new Map<key_datatype, value_datatype>{

Key => value,....};

sObject Syntax:

Map<sObject, value_datatype> map_obj = new Map<sObject, value_datatype>{

Key => value,....};

Here, the sObject can be a standard or custom object. In this entire article, we will only deal with map with the “Account” sObject.

Let’s see the methods that are supported by the Apex “map” collection one by one.

Environment Setup

1. Quickly login to Salesforce and open the “Developer Console” by clicking the gear icon.

2. Then, open the “Anonymous Window” by clicking on “Debug” and “Open Execute Anonymous Window”.

Generic Example:

First, we will see the generic map creation by creating a map with two subjects: the “subject_id” which acts as a key and “value” as the subject name.

Map<Integer, String> programming=new Map<Integer, String>{1=> 'Linux',2=> 'Python'};

system.debug(programming);

Output:

  1. Click on “Execute”.
  2. Check the “Debug Only” option. You can see the output in the “Execution Log”.

Map Methods

First, we create a map from the “Account” object.  We create three accounts with names one by one. Then, we declare a map with the key and value as <sObject, Integer> type and pass the previous three accounts to the map by providing some values.

// Create 3 accounts with Name

Account account1 = new Account(Name='Linux Hint');

Account account2 = new Account(Name='Salesforce');

Account account3 = new Account(Name='Python');

// Add the above accounts as keys to the map_obj

Map<sObject, Integer> map_obj = new Map<sObject, Integer>{

account1 => 1000,account2 => 2000,account3 => 3000};

System.debug(map_obj);

Output:

You can see that “map_obj” stores three accounts.

1. Map.values()

To return only the values from the given map, we can use the values() method. It won’t take any parameters. It simply returns the list of values separated by comma.

Syntax:

map_object.values()

Example:

Let’s return all the values from the previous map. Make sure that you need to execute the previous example code (create a map with three accounts). Otherwise, you will get an error. The code should also exist in the console.

// Return values for all the keys using values()

System.debug(map_obj.values());

Output:

There are only three key:value pairs in the map_obj. The values are: 1000, 2000, and 3000.

2. Map.keySet()

Return the keys are present in the map object. Similar to values(), there’s no need to pass any parameter to this method.

Syntax:

map_object.keySet()

Example:

Let’s return all the keys from the previous map. Make sure that you execute the previous example code (create a map with three accounts). Otherwise, you will get an error. The code should also exist in the console.

// Return all the Keys using keySet()

System.debug(map_obj.keySet());

Output:

There are only three key:value pairs in the map_obj. The keys are: {Account:{Name=Linux Hint}, Account:{Name=Python}, and Account:{Name=Salesforce}.

3. Map.size()

In some scenarios, we need to know the total items (key:value) pairs that are present in the Apex map. Size() is the method that returns the total (key:value) pairs that exist in the map_object. Parameters are not needed for this method.

Syntax:

map_object.size()

Example:

Return the size of the previous map object.

// Return total number of pairs using size()

System.debug(map_obj.size());

Output:

Since there are only 3 pairs, the returned size() is 3.

4. Map.get()

Accessing the values from the map using the key is done using the get() method. To do this, we need to pass the key as a parameter to the get() method. If an unknown key is passed, it returns an error.

Syntax:

map_object.get(key)

Example:

Return the values of  key-2 and key-1 separately.

// get the value of second key

System.debug(map_obj.get(account2));

// get the value of first key

System.debug(map_obj.get(account1));

Output:

Here, 2000 is the value of the “Salesforce” key and 1000 is the value of the “Linux Hint” key.

5. Map.clear()

All the pairs in an Apex map collection can be deleted at a time using the clear() method. It won’t take any parameters.

Syntax:

map_object.clear()

Example:

Remove the pairs in the previous “map_obj”.

//Before clear()

System.debug(map_obj);

// Remove all pairs using clear()

map_obj.clear();

//After clear()

System.debug(map_obj);

Output:

Previously, there are 3 key-value pairs in the “map_obj”. After applying the clear() method, all 3 are deleted.

6. Map.equals()

We can compare two map objects using the equals() method. The Boolean value of true is returned if all keys and values are the same in both map objects. While the Boolean value of false is returned if at least one value is different.

Syntax:

map_object1.equals(map_object2)

Example:

Let’s create three map objects with one key:value pair, each with respect to the “Account” object. Compare these objects among them.

// Account-1

Account account1 = new Account(Name='Linux Hint');

Map<sObject, Integer> map_obj1 = new Map<sObject, Integer>{

account1 => 1000};

System.debug('Map - 1:' + map_obj1);

// Account-2

Account account2 = new Account(Name='Linux Hint');

Map<sObject, Integer> map_obj2 = new Map<sObject, Integer>{

account2 => 1000};

System.debug('Map - 2:' + map_obj1);

// Account-3

Account account3 = new Account(Name='Python');

Map<sObject, Integer> map_obj3 = new Map<sObject, Integer>{

account3 => 2000};

System.debug('Map - 3:' + map_obj3);

// equals()

System.debug('Map 1 & Map 2 are Equal: '+ map_obj1.equals(map_obj2));

System.debug('Map 1 & Map 3 are Equal: '+ map_obj1.equals(map_obj3));

Output:

The first and second map objects are equal since both keys and values are the same in both objects. The first and third map objects are not equal since the keys and values are different.

7. Map.isEmpty()

We can check whether the map is empty or not using the isEmpty() method. True is returned if the Apex map collection is empty. Otherwise, false is returned. Similar to the size() method, it won’t take any parameter.

Syntax:

map_object.isEmpty()

Example:

Let’s create two map objects which are related to “Account” and check whether these two are empty or not.

// Account-1

Account account1 = new Account(Name='Linux Hint');

Map<sObject, Integer> map_obj1 = new Map<sObject, Integer>{

account1 => 1000};

 

// Account-2

Map<sObject, Integer> map_obj2 = new Map<sObject, Integer>();

// isEmpty()

System.debug('Map-1 is empty: '+map_obj1.isEmpty());

System.debug('Map-2 is empty: '+map_obj2.isEmpty());

Output:

The first map is not empty since it holds one key-value pair. The second map is empty since it holds none.

8. Map.remove()

The remove() method in Apex map collection is used to remove a particular key-value pair based on the key which is specified in it as a parameter. If the key does not exist, an error is raised.

Syntax:

map_object.remove(key)

Example:

Let’s create a map with two items and remove the first item.

Account account1 = new Account(Name='Linux Hint');

Account account2 = new Account(Name='Python');

Map<sObject, Integer> map_obj = new Map<sObject, Integer>{

account1 => 1000,account2 => 4000};

System.debug('Existing Map'+ map_obj);

//remove()

map_obj.remove(account1);

System.debug('After removing the first item:'+map_obj);

Output:

After removing the first item from the map, only one item exists – {Account:{Name=Python}=4000}.

9. Map.put()

Using this method, we can directly add one item to the map object at a time. It accepts two parameters: “key” is the first parameter while “value” is the second parameter.

Syntax:

map_object.put(key,value)

Example:

Let’s create a map with one key-value pair. Then, we use the “put” method to insert “account2”.

// Account-1

Account account1 = new Account(Name='Linux Hint');

Map<sObject, Integer> map_obj1 = new Map<sObject, Integer>{

account1 => 1000};

System.debug('Actual Map: '+map_obj1);

// Account-2

Account account2 = new Account(Name='Python');

// put()

map_obj1.put(account2,2000);

System.debug('Final Map: '+map_obj1);

Output:

Previously, there is only one key-value pair in the map which is {Account:{Name=Linux Hint}=1000}. After adding “account2”, the final map holds two key-value pairs which are {Account:{Name=Linux Hint}=1000 and Account:{Name=Python}=2000}.

10. Map.putAll()

Using this method, we can directly add a single or multiple items to the map object at a time. It takes a map collection object as a parameter.

Syntax:

map_object1.putAll(map_object2)

Example:

Let’s create a map with two key-value pairs and create an empty map object again with no items. Use the putAll() method to add the items that are available in the first map object to the second map object.

Account account1 = new Account(Name='Linux Hint');

Account account2 = new Account(Name='Python');

Map<sObject, Integer> map_obj1 = new Map<sObject, Integer>{

account1 => 1000, account2=> 2000};

System.debug(map_obj1);

Map<sObject, Integer> map_obj2 = new Map<sObject, Integer>();

//putAll()

map_obj2.putAll(map_obj1);

System.debug(map_obj2);

Output:

Conclusion

Map is a data structure that is mainly used in trigger scenarios and helps to load more data at a time into the Salesforce database like a list. We have two options to add the items into the map: using put() and putAll(). The remove() method is used to remove a particular item from the Apex map collection. The clear() method is used to delete all items. Also, we learned how to return the values and keys using the values() and keySet() methods.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain