
SAP CPI Groovy Scripting: 10 Patterns Every Integration Developer Must Know
Groovy is the secret weapon of every serious CPI developer. Here are the 10 most useful scripting patterns — with real code you can copy-paste into your iFlows today.


Groovy scripting inside SAP Integration Suite is where you separate iFlow assemblers from true integration developers. Here are 10 patterns you'll use in almost every enterprise integration.
1. Read Payload as String
import com.sap.gateway.ip.core.customdev.util.Message
Message processData(Message message) {
def body = message.getBody(String.class)
message.setProperty('originalBody', body)
return message
}2. Set Custom Headers
Message processData(Message message) {
message.setHeader('X-Correlation-Id', UUID.randomUUID().toString())
message.setHeader('X-Source-System', 'S4HANA')
return message
}3. Parse JSON
import groovy.json.JsonSlurper
Message processData(Message message) {
def body = message.getBody(String.class)
def json = new JsonSlurper().parseText(body)
message.setProperty('customerId', json.customer.id)
return message
}4. Build JSON Response
import groovy.json.JsonBuilder
Message processData(Message message) {
def builder = new JsonBuilder()
builder {
status 'success'
timestamp new Date().format("yyyy-MM-dd'T'HH:mm:ss")
data { orderId 'ORD-12345' }
}
message.setBody(builder.toString())
return message
}The remaining 6 patterns — XML transformation, header-to-property mapping, secure parameter access, exception raising, MPL logging, and Base64 encoding — are covered in-depth in our SAP CPI Mastery course, along with 40 more advanced scripts.

Writing here to help Indian SAP & IT professionals level up faster. Also teaches on IntegrationX Pro.
Ready to level up?
Join 84,000+ Indian SAP professionals learning with us.

