Skip to Content
Technical Articles
Author's profile photo Vikas Jangid

Understanding Groovy Scripting for SAP Integration Suite – Part 1

As a SAP PI/PO developer, it was challenging to migrate to CPI, as we do not get all the functionalities in CPI. One important functionality that we miss is UDFs given by SAP in B2B Mapping Kit, function libraries.

Going ahead with graphical mapping to handle contexts without UDFs is a complex task, and same could be experienced while doing mapping in CPI. Now, only way out was to develop UDFs with CPI provided coding language i.e. Groovy Scripting.

After a good online research, I came across many post where glimpses of Groovy scripts were shared, or java code was requested to be transformed to groovy, but none satisfied my requirement. Many suggests to do the mapping in PI/PO and import the complete mapping in CPI, but I felt it a lame idea to always do so, further more, a why a buyer (client) will buy both PI & CPI system, just to convert some mappings to use in CPI. So, it would not be feasible and neither would always be possible that you would have a PI system to do so. So, I started with hit and trial methods of coding, taking some help from tutorialpoint related to syntax, and various other sources, mentioned later in post, to achieve the needful.

I will share some quick tips & tricks that I took weeks to figure out regarding Groovy scripting. In continuation, will release further blogs with sample codes of UDFs in groovy provided by SAP in PI/PO, for eg. UDFNodePool, UDFUtilsPool etc. Also, as read on various blogs/Q&A, multiple online/offline compilers are available, but I didn’t find them of much use to me, so directly started using CPI compiler, which might feel irritating at times as it take bit longer to process a piece of code, but definitely was of maximum help.

Disclaimer – Please note, this blog is limited to groovy scripting only in CPI and might not work on other platforms. This sequence of blogs is only related to groovy scripting used to develop functions inside mapping level use, and should not be confused with groovy scripting done at integration flow level. The codes mentioned in this blog & follow-up blogs are just sample codes for reference purpose, and shouldn’t be taken as certified programs, and also may be freely edited and used on individual’s requirement. As CPI is continuously evolving, the steps might differ with versions.

*Here after UDFs / Custom Functions will be used interchangeably, both means the same, unless otherwise stated specifically. 

Key Points about Groovy

1. Groovy is developed with following libraries in-built inside so no need to import them once again. ref

  • import java.lang.*
  • import java.util.*
  • import java.io.*
  • import java.net.*
  • import groovy.lang.*
  • import groovy.util.*
  • import java.math.BigInteger
  • import java.math.BigDecimal

2. SAP provides a package com.sap.it.api.mapping that comes with below classes for our use

Interface Summary 
Interface Description
MappingContext
Mapping context object which shall be provided to mapping User Defined Functions
Output
This class is used in advanced user-defined functions (execution type “All values of Context” or “All values of a Queue”) to return the result of a function.
ValueMappingApi
ValueMapping Api will be used to execute value mapping with the given parameters

3. I would suggest to go through below links before you actually start coding

> Groovy Language Documentation

> Groovy Tutorial

4. Groovy in CPI is pre-designed with static void main(String[] args) function, so no need to write it again, else it might give errors.

5. Just focus on writing the functions.

Steps to create a UDF in CPI –

1. Create required flow with a message mapping in it.

2. Assign your desired Source and Target structures

3. click the create icon in Functions section.

>> The icon next to create is for assigning/importing an already created UDF. A groovy code can be copy pasted in notepad, and can be saved with extension name.groovy, which can later be imported here in mapping for use.

4. Give some name to your script, and click ok

5. This creates a sample script with a default code as shown below.

6. The customFunc code mentioned by default, basically passes the input value to the output as it is, without any processing.

7. I would suggest to rename the customFunc as per your script name for now to avoid confusion, will talk about the added functionalities in later part.

8. Click OK on top right corner. This successfully creates your first custom function.

Steps to assign an already created script

This would be useful hereafter, to use code in a notepad, save with name.groovy extension and import in mapping.

Steps 1 & 2 remains same as above

3. click on assign icon, and below window opens. Select the “Upload from File System”, and choose the groovy file to upload.

Alternate Way

Steps 1 & 2 remains same as above

3. go to home page of integration flow -> go to resource tab -> Add -> Script -> Select File System -> Browse & Upload

* Do note, in this method, after uploading script to resource, you might be required to re-assign the script in mapping as in above step with assign icon, but vice-versa is not required i.e. if you upload a script using assign icon in mapping functions, it gets automatically assigned to resource tab.

Conclusion

This was about building base for groovy, and setting up in system, the next blog will be followed with understanding of Groovy Scripting with help of few custom functions codes.

Part 2 >

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Fatih Pense
      Fatih Pense

      Hi Vikas,

      Really nice series of posts about CPI and UDF. Thanks for sharing!

      I think you can add these tags for your current & future blog posts for better visibility

      • SAP Integration Suite
      • Cloud Integration

      Best regards,
      Fatih