Hello Integrators!

 

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?

 

By scripting, I am referring to the use of a scripting language (like Groovy, JavaScript) in your integration projects. SAP Cloud Platform Integration provides a rich set of functionality to transform your data – mapping, content modifier, converters, and so on. However, at times you want to perform a more complex task that lies outside of the native functionality provided. For example, say you receive the following incoming payload:

Sample_In.JPG

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:

Samplt_out.JPG

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?

 

SAP Cloud Platform Integration supports two forms of scripting languages: Groovy and JavaScript

 

  • 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.
  • JavaScript is a dynamic programming language of the Web. Most HTML pages are programmed using

 

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:

 

script_folder.JPG

 

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.

Script_Palette.JPG

 

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.

/wp-content/uploads/2015/01/groovy_explained_625695.png

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

jaxen.JPG

 

Step 2: Include the import definitions in the script file of your integration flow

AddingJARs.JPG

Step 3: Modify the processData function with your function logic

codesnippet.JPG

 

That is it! The procedure to use script in integration flows is simple.

 

Conclusion

 

You can utilise a scripting language in your SAP Cloud Platform Integration project for complex transformations.As language of choice, you have Groovy or JavaScript – and in addition, you can augment it with external libraries. We strongly recommend that you first look at the native functionality supported by the toolset before writing scripts. By its inherent nature, scripts can make integration projects harder to maintain.

 

Best Regards,

Sujit

To report this post you need to login first.

4 Comments

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

  1. Amit Kamat

    Hi Sujit,

    Can you please share the sample code. Also Can you please let me know where can I find the required jar for groovy.

    (0) 
  2. Vipin Kanchan

    Hi Sujit,

    Is creating a context change supported in a grrovy script or UDF in HCI.

    We have a UDF which creates an additional context and splits the value based on specific condition in PI, but when we import it into HCI and run the interface we get only one context instead of getting 2 contexts with split values.

    (0) 
  3. dhivakar muthusamy

    Hi Sujit,

    Thank You very much for the blog. It’s very much helpful!!!
    I have done previously the same requirement by using some context handling. Won’t this be possible in standard Mapping as Graphical mapping in PI.

    Will the same be possible in HCI?

    (0) 

Leave a Reply