Blog 9: Scripting in Integration Flows
In this blog, we shall explore a slightly advanced feature of SAP Cloud Platform Integration (f.k.a HCI) – scripting.
What is Scripting and When to use scripting?
The payload comes from an HR system that tracks based on activities recorded every day. You want to integrate to a time recording system, that expects the total number of work hours against each person. Expected payload is in this format:
So, we have to parse through the entire incoming payload, calculate the number of hours against each person, and map to the final payload format. Achieving this scenario using the native functionality set of SAP Cloud Platform Integration could be a little tedious task. It can be easy accomplished using custom transformation functions. That is where scripting comes in.
Which Scripting Languages are supported?
- Groovy is an Object Oriented Scripting Language which provides dynamic, easy-to-use capabilities. It absorbs most of the syntax from Java. Learn more Groovy from its site here.
Both scripting languages are easy to learn and come with a host of resources that you can use in your integration project.
How to use Scripting?
In the integration project, you should create the following folders in your project for scripting:
The src.main.resource.script folder should contain all the scripts. In an integration flow, the script step is available as part of the Message Transformer step. In the context menu of the script step, you can create or assign existing scripts from the folder.
When you create a new script, you shall get the default code editor in Eclipse with the following view. Functions to access the message are provided to you by default.
Using External Libraries in Scripting
One more cool feature you could use in the scripting message is the use of external libraries in your project.There are many open source libraries available to you.
Let’s say you want read an XML file in Groovy using an Xpath library. And you finalized on using Jaxen. Then, here is how you proceed in an integration flow project.
Step 1: Import the Jaxen libraries in the src.main.resources.lib folder
Step 2: Include the import definitions in the script file of your integration flow
Step 3: Modify the processData function with your function logic
That is it! The procedure to use script in integration flows is simple.