Skip to Content
Product Information
Author's profile photo Priyank Kumar Jain

Using Hashmaps for storing and retrieving data in I-Flows (CPI)

What is a Hashmap?

A hashmap, or a hash table, is a data structure which stores data in the form of Key-Value pairs. By looking up for a key, the corresponding value against that key can be retrieved. How it works? It transforms the key into a hash, using a hash function. A hash is nothing but a number which serves as an index to locate the record in the hashmap, from where the corresponding value against that key can be retrieved.

Typically, a Hashmap support insert, delete and lookup functions. Lookup is the searching and locating of a desired record, fetched against a given key. It finds significant use in extremely large data sets wherein a hashmap makes lookup extremely fast and efficient.

HashMaps in CPI

SAP Cloud Platform Integration (CPI) supports the use of Hashmap variables through the HashMap class which is available and can be used in scripts. By using these variables, data available in (for example) XML nodes can be stored in HashMap, and later retrieved through a lookup.

The CPI already does offer the “Content Modifier” which also helps in storing such data and retrieving subsequently. However, the Hashmap offers the following advantages:

· Offers easy storing of values against a key

· Can be stored in data stores and can thus be accessible across different i-Flows.

· The “key” can be a combination of multiple (concatenated) values, thereby offering multiple values to be used as a key.

· Retrieval (Lookup) is fast and efficient. Should be considered for extremely high data volumes.


Let us have a quick look at an example of how these hashmap variables can be used in a simple groovy script in CPI to store data and subsequently retrieve it.

For the purpose of this document, we will assume a simple XML payload body as a message. This message will have the data which will be looped in a groovy script and stored in hashmap variables. Then the data will be retrieved from this hashmap.

To start, create an integration flow and add a small content modifier. The content modifier can contain the following XML data in the Message Body.













Also add a property in the Content Modifier. This will help to retrieve values from the hashmap and easy monitoring.

Now, after the content modifier, add a script in the I-flow. The code snippet to be used in the groovy script is mentioned below:


import java.util.HashMap;


def Message processData(Message message) {

def body = message.getBody(;

HashMap<Integer, String> hmap = new HashMap<Integer, String>();

def Root = new XmlSlurper().parse(body);



hmap.put(, it.firstname.text().toString());


catch(Exception ex){

//put relevant exception handling here



message.setProperty(“hashmapdata”, hmap);

return message;


The HashMap class is used to declare the hashmap object. The following line in the script

HashMap<Integer, String> hmap = new HashMap<Integer, String>();

declares the hashmap. It takes a key-value pair, the key being an integer type and the value being a string.

Later, the xml payload is looped using the “Each” keyword. Within this loop, the value of the “id” and the “firstname” tags from the xml payload are put into the hashmap variable.

In the end of the code, the property declared earlier as part of the content modifier, is populated with the content of the hashmap. This is only to verify that our data is correctly stored in the hashmap. We copy the content of the hashmap into this property and retrieve in a subsequent script in the form of an attachment (shown below).


import java.util.HashMap;

def Message processData(Message message) {

def map = message.getProperties();

def body = message.getBody(java.lang.String) as String;

String hashmap = map.get(“hashmapdata”);

def messageLog = messageLogFactory.getMessageLog(message);

messageLog.addAttachmentAsString(“Payload post hashmap:”, hashmap, “text/xml”);

return message;


Save the i-flow. Deploying it at this stage will show us an attachment with the content of the property, which in-turn stores the content of the hashmap variable.

As can be seen in the above screenshot, the key-value pair of ID and firstname from the xml message have been retrieved in the attachment from the hashmap.

We will now see how we can lookup the hashmap in the script code itself. This can be done with the “get” method of the hashmap class.

Modify the code in the second groovy script as follows:


import java.util.HashMap;

import groovy.xml.*;

def Message processData(Message message) {

def body = message.getBody(java.lang.String);

def messageLog = messageLogFactory.getMessageLog(message);

map = message.getProperties();

Map<Integer,String> hmap1 = map.get(“hashmapdata”);

//now retrieve value from hashmap

String id = ‘0001’;

String val = new String(hmap1.get(Integer.parseInt(id)));

messageLog.addAttachmentAsString(“FinalOutput:”, val, “text/plain”);

return message;


In the above example, we retrieve the value from the hashmap variable by using a simple key value 0001. Here, this is a simple retrieval just to show that from the “key-value” pair of the hashmap, the value is retrieved by simply specifying the key in the “get” method. The same can be used to get values in more complicated/real-life business scenario as well. The output of the above script is as below:

This was a simple example to demonstrate the use of hashmap variables in CPI.

Assigned tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Cassiano Lins
      Cassiano Lins

      Hi Priyank Kumar Jain nice content!

      Author's profile photo Balaji Singaperumal
      Balaji Singaperumal

      Hi Priyank Jain,

      Nice blog. We can also use the Hashmaps when need to pass payload from one branch from another branch in a multicast. My question what is the limitation to use Hashmap. I mean a property or header can hold large amount of payload for ex 50,000 compound employee records?


      Author's profile photo sudeep menakuru
      sudeep menakuru

      Hi Priyank,

      What would be the logic if we need to extract two outputs fields for one input string?

      Can you please help me out here?

      Thank you!

      Best Regards,

      Sudeep Menakuru.

      Author's profile photo Vivek Vijay
      Vivek Vijay


      What if we need to get the last name as well.

      Will the groovy coded be as below.


      HashMap<Integer, String> hmap = new HashMap<Integer, String>();

      def Root = new XmlSlurper().parse(body);



      hmap.put(, it.firstname.text().toString(), it.lastname.text().toString());



      Thanks in advance,


      Author's profile photo Rashmi Joshi
      Rashmi Joshi

      Hi @priyank,

      So basically I can use hash map if I have large payload and I will use it for the lookup.... please help me to understand how is it different than content enricherbas for lookup I can use this as well...

      Also, In many time we have seen orderID along with multiple lineitems come as input payload. Can I use this class to store whole payload and use ID to fetch all itemdetails?