Common SAP CPI Groovy script errors and how to fix them
The error messages you are most likely to meet in SAP Integration Suite (Cloud Integration, CPI) Groovy scripts, what causes them and how to fix them. The messages below are the real ones produced by Groovy 2.4 and Groovy 4; you can reproduce and debug each case in the Groovebox online Groovy IDE, which highlights the failing line.
unable to resolve class XmlSlurper
startup failed:
Script1.groovy: 3: unable to resolve class XmlSlurper
Cause: the script runs on Groovy 4, where XmlSlurper and XmlParser moved from
groovy.util (imported automatically in Groovy 2.4) to groovy.xml.
Fix: add import groovy.xml.XmlSlurper. The same message for sun.misc.BASE64Encoder
means a Java 8 class that no longer exists on Java 17: use bytes.encodeBase64() or java.util.Base64.
Details in Groovy 2.4 vs Groovy 4.
groovy.lang.MissingPropertyException: No such property
groovy.lang.MissingPropertyException: No such property: orderId for class: Script1
Cause: a variable that was never defined, usually a typo or a variable declared inside another block
(if, closure) and used outside it. Fix: declare it with def in the right scope, or read
the value from the message: message.getProperty('orderId').
Cannot invoke method ... on null object
java.lang.NullPointerException: Cannot invoke method trim() on null object
Cause: a header, property or lookup result that does not exist returns null, and the script calls
a method on it. Fix: use the safe navigation operator or a default value:
def sender = message.getHeader('SAP_Sender', String)?.trim() ?: 'UNKNOWN'
SAXParseException: Content is not allowed in prolog
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
Cause: XmlSlurper or XmlParser received something that is not XML: JSON, plain text, or
bytes before the first < (for example a byte order mark). With an empty body the message is
Premature end of file. Fix: check what the previous iFlow step really produces (a converter,
a request-reply, an encoder) and guard against empty payloads before parsing.
JsonException: Unable to determine the current character
groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
The current character read is '<' with an int value of 60
Cause: JsonSlurper received XML (the character <) or another non-JSON payload.
Fix: convert the payload to JSON first, or parse it with XmlSlurper.
MissingMethodException: No signature of method
groovy.lang.MissingMethodException: No signature of method: ...setHeader() is applicable for argument types: (java.lang.String) values: [OnlyName]
Possible solutions: setHeader(java.lang.String, java.lang.Object), ...
Cause: a method called with the wrong number or types of arguments. Fix: the
Possible solutions line lists the signatures that exist; here setHeader needs a name and a value.
GroovyCastException: Cannot cast object
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'abc' with class 'java.lang.String' to class 'java.lang.Integer'
Cause: assigning a value to a typed variable (Integer n = ...) when it has another type. Headers and
properties set from iFlow steps are often Strings. Fix: convert explicitly, and validate the input:
def raw = message.getProperty('MaxItems')
Integer maxItems = raw?.toString()?.isInteger() ? raw.toString() as Integer : 100
Converting a String that is not a number with as Integer fails with
java.lang.NumberFormatException: For input string: "abc".
No error, but an empty value
With XmlSlurper, a path that does not exist does not fail: order.Missing.Field.text() returns an
empty string and size() returns 0. Check size() (or isEmpty()) when an element is
mandatory, otherwise the script silently writes empty values.
How to debug faster
- Reproduce the failing message: copy the payload, headers and properties from the MPL or a trace into the Groovebox playground.
- Run the script on the same runtime as the iFlow (Groovy 2.4 or Groovy 4): the failing line is highlighted.
- Add
printlnstatements: their output appears in the Console tab. - Fix, run again, and only then deploy.
Working snippets for everyday tasks are collected in SAP CPI Groovy script examples.