Dear CPI folks,
Unfortunately by searching on SAP community, it is very hard to find any resource on how to do that. With some research on this topic, I managed to test successfully with JS script powerned iFlow. That’s the reason why I want to share my findings in the community.
Based on SAP Help Portal document: https://help.sap.com/docs/cloud-integration/sap-cloud-integration/define-local-script-step. The scripting engine that was used in CPI runtime environment is so called ‘Rhino‘.
The API document is here: https://javadoc.io/doc/org.mozilla/rhino/1.7R4/index.html
Local Test Environment
It is always joyful that we can play around with scripting locally before using it officially in our project.
To prepare a playground on your local environment. Download the jar file from https://mvnrepository.com/artifact/org.mozilla/rhino/1.7R4, put it in somewhere on your PC. And then use the command line “java -jar rhino-1.7R4.jar -debug -version 180” to start the shell to test.
I still haven’t figure out what is the difference between these possibile version no: -version 100|110|120|130|140|150|160|170|180. But from the screenshot above you can see, “help()” is very useful to get some idea on how to setup the shell environment and version() will give you exactly what you set for the version.
To test the local script file, you can use “-f” parameter like following:
java -jar rhino-1.7R4.jar -f <filename of your rhino js file>
CPI iFlow Implementation:
Now let’s implement some simple CPI scripting features in the iFlow:
In my test iFlow design,
- The iFlow will be triggered only once by Timer so that the test can be triggered/managed by deployment on demand.
- The XML payload was generated by a SFAPI (SOAP) query to the Compound Employee entity of sandbox SFSF tenant.
- I chained two scripts (JS and Groovy) in order to get the logging message to compare the results.
- The XML Modifier is used to remove the XML head from the payload so that your JS script can work without any issue.
payload = payload.replace(/<\?xml[^>]*\?>/, "");
The script itself is very straight forward. The only thing that I want to mention is that the Rhino JS engine supports the ECMAScript for XML (E4X) which is an extension to provide the native support of XML in Rhino.
So you can easily access the XML payload with single line of code, but please bear in mind if you use “typeof parsedPayload” to check the object type, it is “xml” instead of “object“.
const parsedPayload = new XML(body);
you can get more details about this topic from here: https://svn.wso2.org/repos/wso2/tags/carbon/0.1alpha/mashup/java/xdocs/e4xquickstart.html#modifying
Source Code of my script:
After deployment of the iFlow, I got the successfully result as expected. The total number of Compound Employee records and the first employee payload appear in the attachment.
First Employee Payload:
Simple like that !
I hope this blog can inspire you on using Java Script as the scripting language in your next project. Enjoy it. 🙂