CPI Groovy Examples

Go to List of Examples

Basic Script Example for Beginners

The main entry point is function processData by default. You can change it in the UI.

This function gets Message object defined in com.sap.gateway.ip.core.customdev.util.Message and returns it back.

Message object contains:

  • single body
  • multiple headers
  • multiple properties

It also supports SOAP headers & attachments but for clarity, we will look into that later.

Changing body

In this basic example we don't modify XML using XML libraries. We just treat it as a string.

message.getBody function can return different Java types. There is a related blog post in SAP Community.

Changing headers & properties

They provide similar functions. You can get a Java Map object by calling message.getHeaders() and message.getProperties().

You can use functions on the message object to set new headers, or modify existing ones.

  • For headers: message.setHeader( key , value )
  • For properties: message.setProperty( key , value )


Try it on Groovy IDE
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
    println "You can print and see the result in the console!"
    def body = message.getBody(String);
    message.setBody(body + "Body is modified");
    def map = message.getHeaders();
    def value = map.get("oldHeader");
    println "oldHeader value: " +value
    message.setHeader("oldHeader", value + " World!");
    message.setHeader("newHeader", "newHeader value");

    def mapProperties = message.getProperties();
    def valueProperty = mapProperties.get("oldProperty");
    message.setProperty("oldProperty", valueProperty + "modified");
    message.setProperty("newProperty", "newProperty value");
    return message;

Input Body

<root>Example Input Data</root>

Input Headers


Input Properties

oldPropertyThis property is

Expected Result Body

<root>Example Input Data</root>Body is modified

Expected Headers

oldHeadernull World!
newHeadernewHeader value

Expected Properties

newPropertynewProperty value