CPI Groovy Part 1: The Message API, Headers, Properties and Logging
Lessons are open to try during our launch period. Some advanced content may move to Pro later. AI practice limits apply separately.
Learn the Message object every Groovy script receives: reading and writing the body, headers and exchange properties, choosing between headers and properties, attaching payloads to the message processing log safely, and when not to script at all.
In this lessonShow sectionsHide sections
Visual overview
The situation: a real project scenario
A support team could not find failed orders in monitoring because messages were only identifiable by technical IDs. A three-line script adding the sales order number as a custom header property made every message searchable by business key, cutting investigation time from hours to minutes.
Explanation
This series goes deeper into Groovy scripting in Cloud Integration. Part 1 covers the Message API that every script uses.
The script contract: a Groovy script step calls a function, by default processData(Message message), passing a com.sap.gateway.ip.core.customdev.util.Message object. The script reads or changes the body, headers, properties and attachments, then returns the message. Anything the script throws becomes an error in the iFlow and can be caught by an exception subprocess, so scripts don't need their own try/catch just to surface a failure.
Headers versus properties: headers are part of the message and may be passed to receiver adapters as HTTP headers or JMS properties, so sensitive values placed in headers can leak out to receivers. Exchange properties live only inside the iFlow run and are the right place for intermediate values like IDs, flags and lookup results that the flow needs but the receiver should never see. Use allowed-header lists on adapters to control exactly what leaves the iFlow.
Reading bodies efficiently: message.getBody(String) loads the whole payload into a string, which is fine for small messages. For large payloads, read as an InputStream or Reader and process in a streaming way, or use XmlSlurper on a stream rather than a string. Converting large bodies to strings more than once in the same script is a common cause of out-of-memory errors on big files.
Logging with care: messageLogFactory.getMessageLog(message) lets a script add attachments or custom header properties to the message processing log. Custom header properties are searchable in Monitor Message Processing and are the right way to make business keys like order numbers searchable. Payload attachments should be controlled by an externalized parameter so they can be switched on only when troubleshooting, not left on permanently.
When not to script: content modifiers, routers, converters, filters and mappings cover many needs without code. Scripts are harder for other team members to read and review, and they need their own testing. Choose a standard step whenever one exists, and keep the scripts that remain small, focused and documented.
How the SAP process fits together
SAP PROCESS EXPLORER · Integration: sender to receiver
ERPClimb.comGroovy Scripting Deep Dive — process breakdown
Topic-specific conceptual flow: how key objects and decisions fit together. SAP configurations and exceptional cases can follow different paths.
Selected process stage
Read
Access body, headers and properties through the Message API
Representative SAP learning flow, not a guarantee of system runtime sequence in every scenario.
Code example
import com.sap.gateway.ip.core.customdev.util.Messageimport java.util.HashMap def Message processData(Message message) { def body = message.getBody(String.class) // small payload only; use getBody(InputStream) for large ones def props = message.getProperties() // extract a business key from the payload and store it as a PROPERTY (internal, not sent to the receiver) def orderNumber = (body =~ /<OrderNumber>(.*?)<\/OrderNumber>/)[0][1] props.put("orderNumber", orderNumber) // also set it as a HEADER so it is searchable as a custom header property in Monitor Message Processing message.setHeader("SAP_OrderNumber", orderNumber) // never log the full payload unconditionally in production - gate it behind an externalized parameter if (props.get("logPayload") == "true") { def messageLog = messageLogFactory.getMessageLog(message) if (messageLog != null) { messageLog.addAttachmentAsString("Payload", body, "text/xml") } } return message}Watch out
Common mistakes
Putting sensitive data in headers that get forwarded to receivers: headers travel with the message and can be mapped to HTTP headers or JMS properties on the receiver adapter, so a header holding internal or sensitive data can leak outside the iFlow; exchange properties, which never leave the iFlow run, are the correct place for that kind of value.
Logging full payloads permanently: always-on payload attachments expose personal or confidential data in the trace and fill up log storage quickly, so payload logging should be gated behind a parameter and switched on only while actively troubleshooting.
Reading large bodies as strings repeatedly: calling getBody(String) more than once on a large payload, or holding it as a string while also working with a parsed copy, doubles or triples memory use and is a frequent cause of out-of-memory errors on bigger files; streaming APIs avoid this.
Scripting something a standard step already does: a content modifier, router or mapping step is easier for the next developer to read, review and maintain than an equivalent Groovy script, so scripts should be reserved for logic standard steps genuinely cannot express.
Do this
Best practices
Use exchange properties for internal values and headers only for what the receiver genuinely needs to see, since headers can be forwarded to receiver adapters while properties cannot. Add business keys, such as order or document numbers, as custom header properties so messages are searchable by business key in Monitor Message Processing rather than only by technical message ID.
Gate payload logging behind an externalized parameter so it defaults to off in production and can be turned on temporarily for a specific investigation. Use streaming reads (InputStream or Reader) instead of getBody(String) for large messages to avoid loading the full payload into memory more than once.
Keep scripts small and single-purpose, and prefer a standard step over a script whenever one exists, since standard steps are easier for the rest of the team to read and maintain.
In the interview
Interview angle
Interviewers ask what the difference is between headers and exchange properties, how to log safely and how to handle large payloads in Groovy scripts.
A weak answer says "headers and properties are both just key-value pairs." A strong answer explains that headers can be forwarded to receiver adapters (so sensitive data in headers can leak out) while properties stay inside the iFlow run, and ties this to a concrete rule: internal values go in properties, only what the receiver needs goes in headers, and payload logging is parameter-controlled rather than always on.
Practice this lesson
Original educational case studies for SAP consultants, not official SAP certification exam questions. Verify product edition and implementation details against the current SAP documentation.
Quick check
0 of 3 questions attempted
Question 1 of 3
What is the main difference between a header and an exchange property in Cloud Integration?
Question 2 of 3
How can a business key such as an order number be made searchable in message monitoring?
Question 3 of 3
Why should payload attachments in the message log be controlled by a parameter?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1How do you write Groovy scripts in Cloud Integration that are safe for production?
Frequently asked questions
What is the difference between headers and exchange properties in Cloud Integration?
Headers travel with the message and may be passed to receivers, so sensitive values can leak. Exchange properties live only inside the iFlow run and suit intermediate values like IDs, flags and lookup results.
How do you make messages searchable by business key?
Add the key, for example an order number, as a custom header property through the message log in a script. Support can then search monitoring by that key.
How should large payloads be read in Groovy?
Read the body as a stream or Reader instead of converting it to a string repeatedly; repeated full string conversions of large bodies are a common cause of out-of-memory errors.
Was this lesson helpful?
Keep exploring
More lessons in this topic
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.