Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
dominic4
Active Participant
8,645
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 Pos...). 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.
9 Comments
brunoambrozio
Participant
0 Kudos
Very useful Dominic! Thank you for sharing it!
sugandhan11
Explorer
praneethg1
Explorer
0 Kudos
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
former_member132870
Participant
0 Kudos
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
praneethg1
Explorer
0 Kudos
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
former_member132870
Participant
0 Kudos
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
0 Kudos

Hello 6c74addc374841d587eccd4a68075de8

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.

dominic4
Active Participant
0 Kudos
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
0 Kudos
Hi Dominic!

 

Thank you for your replay.

Hope this is available soon.

 

Regards,