Groovy Examples

Go to list of examples

Basic Script Example for Beginners

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:

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.

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

Key Value
oldHeader Hello!

Input Properties

Key Value
oldProperty This property is

Expected Result Body

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

Expected Headers

Key Value
oldHeader Hello! World!
newHeader newHeader value

Expected Properties

Key Value
newProperty newProperty value
oldProperty This property is modified