Value Mappings in SAP CPI are a powerful tool for converting a set of input values to a corresponding set of output values. This is essential for a variety of tasks, such as mapping legacy system codes to new system codes or translating country codes. While you can use the built-in Value Mapping step in an Integration Flow, a Groovy script provides more flexibility and control.
The Value Mapping Table
Before you can read a value from a Groovy script, you need to have a Value Mapping table configured in your Integration Flow artifact. A typical Value Mapping table has a Source Agency, a Source Identifier, a Target Agency, and a Target Identifier.
For example, you might have a table to map country codes:
- Source Agency: 'External_System'
- Source Identifier: 'Country_Code_In'
- Target Agency: 'SAP_CPI'
- Target Identifier: 'Country_Code_Out'
With entries like this:
| Source Value | Target Value |
|---|---|
| IN | India |
| DE | Germany |
Example: Using HashMap to Map and Transform Data
Let's consider a common scenario in SAP CPI: transforming a vendor's country code into a full country name.
<root> <row> <City>Delhi</City> <Country>IN</Country> </row> <row> <City>Berlin</City> <Country>DE</Country> </row> </root>
Groovy Script to Read the Value Mapping:
This script read the value mapping for corresponding country code and modify the xml with its full name.
import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.ITApiFactory;
import com.sap.it.api.ITApi;
import com.sap.it.api.mapping.ValueMappingApi;
def Message processData(Message message)
{
def body = message.getBody(java.lang.String);
def list = new XmlParser().parseText(body);
def valueMapApi = ITApiFactory.getApi(ValueMappingApi.class, null);
def sourceAgency = "External_System";
def sourceIdentifier = "Country_Code_In";
def targetAgency = "SAP_CPI";
def targetIdentifier = "Country_Code_Out";
list.row.each
{
def sourceValue = it.Country?.text()
def mappedValue = valueMapApi.getMappedValue(sourceAgency, sourceIdentifier, sourceValue, targetAgency, targetIdentifier);
if(mappedValue !='')
{
it.Country[0].value = mappedValue;
}
}
message.setBody(groovy.xml.XmlUtil.serialize(list));
return message;
}
Final outcome after the Groovy Script:
As you can see the message have been transformed for Country field with its full name.
<root> <row> <City>Delhi</City> <Country>India</Country> </row> <row> <City>Berlin</City> <Country>Germany</Country> </row> </root>