Skip to Content
Author's profile photo Dominic Beckbauer

Key-Value Mapping in SAP Cloud Platform Integration (CPI) with an integration Flow

Hello Integrators,

with inspiration from the blog post “Key-Value Mapping in CPI for huge data sets has never been easier” from my colleague Martin Pankraz, I changed his code a bit to put it in an integration Flow so I do not need to start Eclipse all the time and to use it in with the whole team.

I use the script in an HTTP-Input integration flow (see my blogpost: Working with HTTP-Input in SAP Cloud Platform Integration Flows and How to Test These Flows with Postman). The payload in Postman is the csv-text and I have four headers for the agency and identifier. The output is than the generated key-value mapping xml.

This is the modified groovy-Script to create the xml-mapping.

//original from Martin Pankraz and modified by Dominic Beckbauer
//

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.MarkupBuilder

def Message processData(Message message) {
       def map = message.getHeaders();
      
      
        def agency1 = map.get("agency1").toString();
        def agency2 = map.get("agency2").toString();
        def identifier1 = map.get("identifier1").toString();
        def identifier2 = map.get("identifier2").toString();       
        
        
         def encoding = message.getBody(java.lang.String);

		def stringPerCode = [:];
		/***********************
			Cheat sheet for pre-processing of CSV:
			([\s]{2,})(?=[aA-zZ])
			([_]{2,})
			
		*/
		encoding.splitEachLine(";") {fields ->
			def listOfItems = [];
			
			fields[1] = fields[1].toString().trim();
			if(fields[1].equals("")){
				fields[1] = "Unbekannt";
			}
			if(fields[1].toString().length() > 60){
				fields[1] = fields[1].substring(0,60);
			}
			fields[1] = fields[1].toString().replaceAll(/[\s]{2,}/,' ');
			
			if(stringPerCode.containsKey(fields[1])){
				def alreadyListed = stringPerCode.get(fields[1]).contains(fields[0]);
				if(!alreadyListed){
					stringPerCode.get(fields[1]) << fields[0];
				} 
			}else{
				listOfItems << fields[0];
				stringPerCode[fields[1]] = listOfItems;
			}
		}
			
		def stringWriter = new StringWriter();
		def mappingBuilder = new MarkupBuilder(stringWriter);
		
		mappingBuilder.mkp.xmlDeclaration(version: "1.0", encoding: encoding)
		
		mappingBuilder.root{
			vm(version:'2.0'){
				stringPerCode.each{code, strings ->
					strings.eachWithIndex { item, index ->
						group(id:UUID.randomUUID().toString()) {
							if(index == 0){
								entry(isDefault:'true') {
									agency(agency1)
									schema(identifier1)
									value(item)
								}
								entry() {
									agency(agency2)
									schema(identifier2)
									value(code)
								}
							}else{
								entry{
									agency(agency1)
									schema(identifier1)
									value(item)
								}
								entry{
									agency(agency2)
									schema(identifier2)
									value(code)
								}
							}
						}
					}
				}
			}
		};
		
		
		message.setBody(stringWriter.toString());
    
    return message;
}

You can find the original Version from Martin here

My Integration flow looks like this:

And not to forget the Postman POST-Request-Header:

and body:

This way you have a nice way to create a value mapping xml without starting eclipse.

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Bruno Brigantini Ambrózio
      Bruno Brigantini Ambrózio

      Very useful Dominic! Thank you for sharing it!

      Author's profile photo Sugandhan Vazhumuni
      Sugandhan Vazhumuni

      Very useful blog. Also refer this blog https://blogs.sap.com/2018/07/23/key-value-mapping-in-cpi-for-huge-data-sets-has-never-been-easier/

      Author's profile photo Praneeth Gollapudi
      Praneeth Gollapudi

      Hello Dominic,

       

      Thanks for the Blog.

      i'm using this script and trying to send xml file via Postman. But getting error.

      Error is due to missing of encoding in the PostMan Body.

       

      What should i be placing in Encoding? Can you please help.

       

      Note: My value mapping has more that 100 values.

       

      Regards

      Praneeth

      Author's profile photo Dominic Beckbauer
      Dominic Beckbauer

      Hi,

      at which place does the error occur?

      Encoding is mostly utf8. So make sure the content that you send in postman is utf8. You can check and change this for example with notepad++

      Best regards

      Dominic

      Author's profile photo Praneeth Gollapudi
      Praneeth Gollapudi

      Hi Dominic,

      Please Find attached screenshot. This is my Input message from Post Man Body.

      And this is the error i can see in CPI.

       

      Regards

      Praneeth

      Author's profile photo Dominic Beckbauer
      Dominic Beckbauer

      If you already have XML, you can download your mapping and modify the files. Than upload again. My script is only if you have an csv input.

      See: https://blogs.sap.com/2018/07/23/key-value-mapping-in-cpi-for-huge-data-sets-has-never-been-easier/

      I hope this helps

      Author's profile photo Vivian Smid
      Vivian Smid

      Hello Dominic Beckbauer

      Thanks for the Blog!

      Is there a way to send the xml file at the Value Mapping already created, directly from CPI?

      For example: We have this rountine in groovy to get the values dynamically:

      ITApiFactory.getApi(ValueMappingApi.class, null);
      String val= service.getMappedValue(sAgency, sSchema, key, tAgency, tSchema);

      Is there a way to do something like that but to setting the values? Or another way with only the IFlow?

      Regards.

      Author's profile photo Dominic Beckbauer
      Dominic Beckbauer
      Blog Post Author

      Hi Vivian Smid,

      this was also a question in one of my projects and it seems like it is planned to create api access to the value mapping but it is not released right now (and nobody knows when)

      BR

      Dominic

      Author's profile photo Vivian Smid
      Vivian Smid

      Hi Dominic!

       

      Thank you for your replay.

      Hope this is available soon.

       

      Regards,