Skip to Content

Dynamic Value Mapping in SAP CPI

Introduction:

Recently came across multiple SCN threads looking for how to deal with dynamic value mapping in SAP CPI,so this is how you can achieve the same with small example.

Scenario:

We will use POSTMAN HTTP client to push some message and get the transformed output using value mapping api ( after value mapping performed ).

Creation of Integration Artifact:

Create an IFlow having below steps as described.

Step 1:

Create a HTTP Sender Communication Channel

Step 2:

Add mapping step following below.

Input and Output Structure:

Mapping Overview of field containing value mapping:

Create a custom function to call the ValueMappingApi Mapping Java API[ Mapping API ] which internally calls the Value Mapping project and gets the required output.

Creation of Custom Function:

Custom Function Script:

Copy paste the below code in Window opened in #4 of Custom function creation.

import com.sap.it.api.mapping.*;
import com.sap.it.api.ITApiFactory;
import com.sap.it.api.ITApi;
import com.sap.it.api.mapping.ValueMappingApi;

/*Add MappingContext parameter to read or set headers and properties
def String customFunc1(String P1,String P2,MappingContext context) {
         String value1 = context.getHeader(P1);
         String value2 = context.getProperty(P2);
         return value1+value2;
}

Add Output parameter to assign the output value.
def void custFunc2(String[] is,String[] ps, Output output, MappingContext context) {
        String value1 = context.getHeader(is[0]);
        String value2 = context.getProperty(ps[0]);
        output.addValue(value1);
        output.addValue(value2);
}*/


def String dynamicValueMap(String sAgency, String sSchema, String tAgency, String tSchema, String key){

def service = ITApiFactory.getApi(ValueMappingApi.class, null);

if( service != null) {

String val= service.getMappedValue(sAgency, sSchema, key, tAgency, tSchema);
if ( val.equals("") || val ==null )
{
    return "default"
}
else
    return val
}

return null;

}

Step 3:

Add intermediate step ( optional ) Write if you want to check the transformed output ( although you get it as response in POSTMAN ).

Step 4:

Save and Deploy your IFlow.

Creation of Value Mapping Artifact:

Create value mapping which can be referred dynamically in your script based on the input you have sent.

My value mapping looks like ( simple one:) )

Save and deploy it.Now we are good to go with our testing.

Testing with some successful transformation screenshots:

Further Enhancements:

  • Even further enhancements can be done to this scenario instead of passing schema & agency you can send some unique no and in later stage of transformation( Custom Function ) manipulate which Value Mapping to be used.
  • You can handle failure case of value mapping ( in our case I have hard coded “default” for failure case) by passing custom key or default value in input payload.

Reference:

[1] Java Mapping API Help

 

Hope this is helpful..!

Regards,

Sriprasad Shivaram Bhat

7 Comments
You must be Logged on to comment or reply to a post.