Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
ranchhod
Explorer

Hello Everyone,

In our work with CPI (Cloud Platform Integration) MPL (Message Processing Logs), we often encounter the need to log specific values such as IDoc Numbers, PO Numbers, and Invoice Numbers as key-value pairs in custom headers. These custom headers allow us to enhance monitoring capabilities.

Traditionally, we would create headers or properties in a content modifier and then read those headers or properties in a custom Groovy script. However, this approach requires us to hardcode the property names both in the custom header and the Groovy script. As new requirements arise, we find ourselves repeatedly adding properties to the Groovy script, which can become cumbersome and error-prone.

To break out of this endless loop, I propose a solution:

  1. Property Naming Convention:

    • When creating properties for custom headers, start their names with “mpl-”. For example:
      • mpl-IDocNumber
      • mpl-PONumber
      • mpl-InvoiceNumber
        ranchhod_4-1714762208521.png
  2. Dynamic Groovy Script:

    • Instead of hardcoding property names in the Groovy script, create a script that dynamically reads all properties starting with “mpl-” from your integration.
    • After trimming the first 4 characters (i.e., “mpl-”), use the remaining part as the key for logging in the MPL custom header.

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 mProp = message.getProperties();                                                                           //Read all the properies
def messageLog = messageLogFactory.getMessageLog(message);
if(messageLog != null)
{
       mProp.each { key, value ->
                            if (key.startsWith("mpl-"))
    {
                                                                                                            
         def customHeaderName = key.substring(4); // Remove "mpl-"                      // Extract the part after "mpl-"

         messageLog.addCustomHeaderProperty(customHeaderName, value)        // Set custom header property
}
}

Output would be like: 

ranchhod_5-1714762598027.png

Now, whenever you need to print something with custom header key-value pairs, you no longer have to hardcode any of the headers or properties within the Groovy code. Instead, you can use a single Groovy script for every interface in your tenant. 👍

By following this approach, you can log additional information without modifying the Groovy script each time a new requirement arises. It provides flexibility and simplifies maintenance.

Labels in this area