We often come across this very strong desire plagued by our laziness that I cannot log payloads in SAP CI but what if I was not lazy enough to implement User defined Message Search on the inbound XML for 2-3 fields in all my iFlows…..What If I could do it. Wouldn’t it make my life much easier.

But then the realization dawns on us that yes to do that we will have to first

  1. Create externalized properties in a content modifier with type as XPATH
  2. Enter the XPATH in the value for each field.
  3. Then individually add the xpath values to the message log as Custom headers.

Problem with this is that if you need to add a new user defined search criteria you need to modify the groovy script and the content modifier.

Imagine you are in productive environment and live and suddenly you observe there is an issue with an existing Integration, but you need to be able to see the values coming through on the run, following the steps 1 to 3 again in Dev and transporting to prod is not an option anymore.

Solution:

This can be easily solved with a two-step process.

 

Step 1: Add a Search Attribute list or the display header name in the Content modifier property section.

Step 2: Add a xpath list for those attributes in the content modifier property section, i.e. the xpath from where those attributes get called.

Content%20Modifier

Content Modifier

Step3: Add the header names you would like and the source XPATHS in the respective properties in a # separated fashion.

Configure%20section

Configure section

Step 4: Trust in groovy and let it do the rest.

 

This groovy script will parse through the Header names and the corresponding XPATHS and create Message Log Headers dynamically.

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

import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilderFactory


def Message logKeys(Message message) {
    
def xml = message.getBody(java.lang.String) as String;
    
    def properties = message.getProperties();
    
    def search_attributes = properties.get("search_attributes");
    def attribute_xpaths = properties.get("attribute_xpaths");

    def messageLog = messageLogFactory.getMessageLog(message);

     String[] search_attributes_array = search_attributes.split("#");
     String[] attribute_xpaths_array = attribute_xpaths.split("#");
     


    if(search_attributes_array.length == attribute_xpaths_array.length )
    {
        def xpath = XPathFactory.newInstance().newXPath()
        def builder     = DocumentBuilderFactory.newInstance().newDocumentBuilder()
        def inputStream = new ByteArrayInputStream( xml.bytes )
        def records     = builder.parse(inputStream).documentElement
        
  
        for(int i=0; i<attribute_xpaths_array.length; i++)
        {
            String temp_value;
            
  
           temp_value = xpath.evaluate( attribute_xpaths_array[i], records )
            
            if(temp_value != null)
            {
                messageLog.addCustomHeaderProperty(search_attributes_array[i], temp_value);
            }

        }
    }

    return message;
}



 

Step 5:

Post your XML towards the iFLOW

<invoice>
      <invoiceHeader  InvoiceNumber="59864799">
        <dueDate>1st May 2023</dueDate>
        <companyCode>100</companyCode>
      </invoiceHeader>
</invoice>

 

Result:

We now have fully automated and configurable Custom Message Headers that we can search in message monitoring.

 

Message%20Monitoring

Message Monitoring

 

 

We already have a few blogs around this topic but none of them covers configurable custom Message headers hence this blog.

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x