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:
Hope this is helpful..!
Regards,
Sriprasad Shivaram Bhat
Hi Sri,
Good one !! Keep blogging !!
thanks and regards,
Praveen T
Nice info Sriprasad and thanks for details. This is will be huge for community.
-Bhargav
Hi Sriprasad,
I'm glad that you helped out, like most of the times 🙂
Thanks a lot
Regards,
Thouheed
Glad it helped!! I think now you can close the theead
Regards,
Sriprasad
Hi Sriprasad,
Thanks for Info, For this scenario Value Mapping and IFLOW artifacts should be in same package or it will work in different package?
Hello Aditya,
There is no dependency as such ,it works if Value Mapping and Integration Project are present in different packages.
Regards,
Sriprasad Shivaram Bhat
Hello Experts,
Can you please tell me how to create a value mapping from the csv file or valuemapping.xml file automatically. As above blog for getting the value mapping entry by passing the source value.My scenario, as SuccesFactors daily runs by query and need to update/create the value mapping table. Any suggestions are appreciated. Thanks
Hi Sriprasad,
is it possible to map one source field to multiple source fields using a custom function ? What I need to reach is the following :the custom function is making API call to get a few values that have to be mapped to multiple source field.
thanks, Radim
Hi Sriprasad,
Thanks for this information. Is there anyway we can identify if a particular value mapping entry is used by a particular interface?
Hi Sriprasad Shivaram Bhat
I tried your coding in a normal groovy script, outside of the mapping. Unfortunately it doesn't work there:
https://tinyurl.com/y38auwqn
Do you any idea how to resolve this?
Thanks,
Chris
Hello Chris,
Try running it in SAP CPI with simple Iflow and please let me know for any issues will try to check from my side also.
Regards,
Sriprasd Shivaram Bhat
Yeah, it does work! Should have tried that first, thank you!!
Dear Sriprasad,
I am a very beginner to CPI, I tried the same step by step execution as shown in your blog. But i am not getting the desired output. I think the value mapping call is not happening. Below is the output i am getting.
Input Value:
<Root>
<valueMapHeader>
<sAgency>DVAgency</sAgency>
<sSchema>DVSchema</sSchema>
<tAgency>DVTAgency</tAgency>
<tSchema>DVTSchema</tSchema>
<Key>keyValue</Key>
</valueMapHeader>
<record>
<SFData1>1</SFData1>
<SFData2>2</SFData2>
<SFData3>3</SFData3>
</record>
</Root>
output Value:
just so i understand. this reads data from the value mapping. Not adds value mapping right? I have a requirement to add automatically some values in the value mapping table. Can this be achieved?
Regards,
Bogdan