About

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 )

Script

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!"
    //Body 
    def body = message.getBody(String);
    message.setBody(body + "Body is modified");
    
    //Headers 
    def map = message.getHeaders();
    def value = map.get("oldHeader");
    println "oldHeader value: " +value
    message.setHeader("oldHeader", value + " World!");
    message.setHeader("newHeader", "newHeader value");

    //Properties 
    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

KeyValue
oldHeaderHello!

Input Properties

KeyValue
oldPropertyThis property is

Expected Result Body

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

Expected Headers

KeyValue
oldHeadernull World!
newHeadernewHeader value

Expected Properties

KeyValue
newPropertynewProperty value
oldPropertynullmodified