Groovy examples for beginners and SAP CPI
These three short examples take you from a standard Groovy script to processing an SAP CPI message. Each includes complete code, input data and the expected result.
By Contiva GmbH · Published October 16, 2026 · For Groovy 2.4.21 and 4.0.29
Choose “Open example in editor” to load the code and input. Select the Groovy version if needed, then click Run. The editor is designed for desktop browsers; you can read or copy the examples on a phone as well. Both language versions use the same tested code and sample data.
Pure Groovy mode
Groovy basics: lists and closures
Filter a list, double the selected values and sum the result. findAll selects elements, collect transforms them and sum adds them together.
Groovy code
def zahlen = [1, 2, 3, 4, 5]
def ergebnis = zahlen.findAll { it > 2 }.collect { it * 2 }
println ergebnis
println ergebnis.sum()Expected console output
[6, 8, 10] 24
The values 3, 4 and 5 match the condition. Doubling them produces [6, 8, 10], whose sum is 24. No SAP classes are needed. The variable zahlen holds the input numbers and ergebnis holds the result.
Open example in editorSAP CPI mode
SAP CPI: read JSON and change the message body
JsonSlurper reads a JSON message body. The script extracts the order number from auftrag and the status, then writes a new body to the Message object.
Input message
{"auftrag": "4711", "status": "neu"}Groovy code
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonSlurper
def Message processData(Message message) {
def daten = new JsonSlurper().parseText(message.getBody(String))
message.setBody("Auftrag " + daten.auftrag + ": " + daten.status)
return message
}Expected message body
Auftrag 4711: neu
In CPI mode, processData must return the Message object. getBody(String) reads the body as text and setBody replaces it with the result. The sample uses German field names and produces the text “Auftrag 4711: neu” (order 4711: new).
Open example in editorSAP CPI mode
SAP CPI: set headers and exchange properties
A script can change headers and exchange properties as well as the body. This example stores the order number in a header and the processing status in a property.
Input message
4711Groovy code
import com.sap.gateway.ip.core.customdev.util.Message
def Message processData(Message message) {
def auftrag = message.getBody(String).trim()
message.setHeader("Auftragsnummer", auftrag)
message.setProperty("Verarbeitet", "ja")
message.setBody("Auftrag " + auftrag + " verarbeitet")
return message
}Expected message body
Auftrag 4711 verarbeitet
Alongside the body, the output contains the header Auftragsnummer with the value 4711 and the property Verarbeitet with the value ja. These mean order number and processed: yes. Check all three results in the editor’s output panels.
Open example in editorCommon mistakes when moving scripts into SAP CPI
- Wrong mode: Scripts importing the SAP Message class belong in CPI mode. Basic list and closure examples can run in Pure mode.
- Missing input: The JSON example expects valid JSON in Input Message. An empty body or invalid JSON causes a parsing error.
- Missing return: The CPI function must return the Message object. End it with return message.
- Different runtime: Test with the Groovy version of your script step. A successful Groovy 4 compile check does not replace testing the complete iFlow.
These examples use synthetic data without external connections. Adapters, target systems and other SAP services are outside the simulation. Test the finished script in SAP Integration Suite as well.