, ,

SAP CPI – Reusable Groovy Script to Log Payload as Attachments


min read



I had previously blogged here with a sample Groovy Script HCI – Payload Logging using Groovy Scripts – Part 1 on how you can log to CPI Payload as Attachments. This is a 7 year old blogpost of mine and is still relevant but with newer features of CPI, there is a need to re-visit this.

Problems with my Previous Approach

  • Script has Attachment Name Hardcoded. So every time you need to change the Attachment Name, you need to copy the script and create a new script
  • CPI since has introduced the concept of “Script Collection” where you can write a script once and then use it across all your Integration Flows.
  • You do not have the option to turn the logging on and off on demand at the Iflow Level.
  • You do not have the option to provide the Attachment Name Dynamically.

The Approach

  • Use a Script Collection.
  • Use a externalized property: logPayload to control whether you want to Log Your Payload or not.
  • Use a property attachmentName to provide names to your attachment dynamically. Use the technicalId of the step if you do not want a dynamic name.

Updated Script

I have since addressed this shortfalls in my various projects and this re-usable script can now be something you can use as a reference.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String
    def map = message.getProperties()
    //Get the StepId of the execution
    def mpl = message.getProperty('SAP_MessageProcessingLog')
    Set mplKeys = mpl.getContainedKeys()
    def stepId = mpl.get(mplKeys.find { it.getName() == 'StepId' })
    //GetAttachmentName Property. If this is not set (NULL), then set AttachmentName to StepId
    String attachmentName = (map?.get("attachmentName")?:stepId)
    //Get Property logPayload. If NULL, set to false
	String logPayload =(map?.get("logPayload")?:'false')
	def messageLog = messageLogFactory.getMessageLog(message)
    if(messageLog != null){
            messageLog.addAttachmentAsString(attachmentName, body, "text/plain")
    return message;
  • The Script expects a “Property” called “attachmentName” to be defined. This is the name of the Attachment in CPI Message Processing Log.
  • If this Property is not found, the AttachmentName is defaulted to the technicalID ( StepID) of the Step.

Create Script Collection in CPI

Create a Script Collection in CPI in any of your Package. I would recommend to create a Utility Package called UTIL_Logging or something equivalent.

Call the Script Collection any name- UTIL_LogPayloadAsAttachment in my case. Use the code above in the script collection.

Use This Script Collection in your Iflows

Reference this Script in your Iflow

Everytime you want to log your payload,

  • Mandatory: Define a Content Modifier step with a property: logPayload with “Externalized” Value.
    • true : Will Log the payload
    • false: Will not log the payload
  • Optional: Add a Content Modifier step with a Property  attachmentName with the name of your attachment and call this Script.

Example Usage 1 : Attachment Name is provided in your Content Modifier

Example Usage 2 : Attachment Name is not provided in your Content Modifier

Turn of Payload Logging at Iflow Level

Change the value of the Externalized Parameter logPayload to false.

Final Thoughts

Payload logging is inevitable and to be used prudently. This approach provides a reusable script to log payload on demand as needed without having to write the code over and over again.

While you have a control at a Iflow Level to turn off and on the logging, it would be a a handy option to also have a control this at a Tenant Level, i.e, if you would like to turn off Logging in your tenant you should be able to do that without going into each Iflow and turning off Logging. See this post for how that can be done – SAP CPI – Reusable Groovy Script to Log Payload as Attachments – Turn off Logging at a Tenant Level