, , ,

SAP CPI – Reusable Groovy Script to Log All EDI Headers

By.

min read

Share

Introduction

  • Have you used Custom Headers in CPI and you love them?
  • Do you use EDI Files in CPI and want to be able to search in CPI using all EDI Headers?
  • Do you want to a reusable groovy script for Logging all EDI Headers as Custom Headers to search for them in CPI?

SAP CPI EDI to XML Convertor

Have you had a look at the headers of your messages after you perform a EDI to XML Conversion? SAP adds a lot of headers that help identify your EDI message to the Headers of your message.

These are essentials headers that help you identify the message from a monitoring and operations perspective. What if you want to log these as custom headers for you to search your messages in CPI?

Reusable Groovy Script

If you notice keenly you will notice that all EDI Headers have these 2 prefixes

  • EDI_
  • SAP_EDI_

This reusable groovy script scans all Message Headers that starts SAP_EDI_ or EDI_ and logs these headers as Custom Message Headers to your Message Processing Log – MPL in CPI.

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

def Message processData(Message message) {
    
	def messageLog = messageLogFactory.getMessageLog(message);
	def headers = message.getHeaders();
	if(messageLog != null){
        headers.each { key, value ->
            if (key.startsWith("SAP_EDI") || key.startsWith("EDI_")) {
                messageLog.addCustomHeaderProperty(key, value);
            }
        }
	}
	return message;
}

Log Limited EDI Headers

While it is cool to be able to log all EDI Headers, the reality is there are too many and add no value. Here is a updated script that logs only specific EDI headers that I have thought are essential from a Monitoring perspective.

  • EDI_Interchange_control_number
  • EDI_Message_Release
  • EDI_Message_Type
  • EDI_Receiver_ID
  • EDI_Receiver_ID_Qualifier
  • EDI_Sender_ID
  • EDI_Sender_ID_Qualifier
import com.sap.gateway.ip.core.customdev.util.Message;

def Message processData(Message message) {
    
	def messageLog = messageLogFactory.getMessageLog(message);
	def headers = message.getHeaders();
    // List of EDI headers to Log
    def keys = [
        'EDI_Interchange_control_number',
        'EDI_Message_Release',
        'EDI_Message_Type',
        'EDI_Receiver_ID',
        'EDI_Receiver_ID_Qualifier',
        'EDI_Sender_ID',
        'EDI_Sender_ID_Qualifier'
    ]
    keys.each { key ->
        if(headers[key]) {
           messageLog.addCustomHeaderProperty("${key}", "${headers[key]}");
        }
    } 
	return message;
}

Final Thoughts

  • This is just an example to show how you can log all your Custom EDI Headers.
  • Lot of these are redundant and I would suggest to reduce the number of EDI headers you log.
  • Ofcourse ensure that this is standardized and used as a Script Collection in your landscape so all EDI Conversions use this.