Essential Groovy Scripts & UDFs For SAP Cloud Integration
Essential Groovy Scripts & UDFs For SAP Cloud Integration
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap
return message
})
message.setProperty(key, value)
}
return message
}
import com.sap.gateway.ip.core.customdev.util.Message
This Message class is a built-in SAP CPI API that allows you to access and manipulate
message content, headers, and properties in an Integration Flow (IFlow).
https://www.linkedin.cin/sakshi-tekale/
Sakshi Tekale
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap
https://www.linkedin.cin/sakshi-tekale/
Sakshi Tekale
3. Dynamic JSON to XML Conversion
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder
xml.root{
json.each{key,value->
"$key"(value)}
}
message.setBody(writer.toString());
return message
}
JsonSlurper is a built-in Groovy class used to parse JSON strings into a Groovy
data structure (usually a Map or List).
In this script, it converts the JSON body of the message into a Map for easy
traversal.
https://www.linkedin.cin/sakshi-tekale/
Sakshi Tekale
4. Dynamic XML to JSON Conversion
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder
xml.root {
buildXML(xml, json)
}
message.setBody(writer.toString())
return message
}
https://www.linkedin.cin/sakshi-tekale/
Sakshi Tekale
}
}
} else {
xml."$key"(value)
}
}
}
groovy.xml.XmlSlurper
This Groovy class is used to parse XML documents easily and turn them into
traversable data structures (similar to DOM but more lightweight).
groovy.json.JsonOutput
This class is used to:
● Convert Groovy Maps or Lists to JSON
● Format JSON strings nicely (pretty print)
Key methods:
● toJson(map) → Converts Map to raw JSON string
● prettyPrint(json) → Makes the JSON readable
5. Capture Message Payload Without Enabling Trace
import com.sap.gateway.ip.core.customdev.util.Message;
if(messageLog != null)
{
messageLog.setStringProperty("log1","Printing Payload As
Attachment")
messageLog.addAttachmentAsString("log1",body,"text/plain");
}
return message;
}
https://www.linkedin.cin/sakshi-tekale/
Sakshi Tekale
messageLogFactory.getMessageLog(message)
This is used to create a logging handle (MessageLog) for the current message.
messageLog.setStringProperty(String key, String value)
● Adds the payload (or any string) as an attachment in the message log.
● You can view this attachment later from Monitor → MPL → Attachments
tab.
import com.sap.gateway.ip.core.customdev.util.Message
message.setBody(body)
return message
trim() removes whitespace characters from the beginning and end of a string.
https://www.linkedin.cin/sakshi-tekale/
Sakshi Tekale
import com.sap.gateway.ip.core.customdev.util.Message
def body=message.getBody(String)
body=body.toUpperCase()
message.setBody(body)
return message
toUpperCase()
● Standard Java/Groovy method from the String class.
● Converts all characters in the string to uppercase
https://www.linkedin.cin/sakshi-tekale/
Sakshi Tekale
Use Case: Assign a default value when input is null or empty.
2. Format Date
import java.text.SimpleDateFormat
Use Case: Convert dates like "2025-04-10" into "10-Apr-2025" or any
format required.
import java.text.SimpleDateFormat
https://www.linkedin.cin/sakshi-tekale/
Sakshi Tekale
import com.sap.it.api.mapping.*;
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.ZoneId
ZonedDateTime.now(ZoneId.of("Asia/Kolkata"))
gets the current date and time in IST.
ZoneId.of("Asia/Kolkata")
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
formattedDate
https://www.linkedin.cin/sakshi-tekale/