Skip to Content
Author's profile photo Shashank Kesarwani

SAP CPI : Accessing Header and Property

Intro: Once you are working on more complex logic to be implemented in CPI, using header and property value is very common practice. Accessing these values in groovy script is known to every one. This blog will go through different ways to access Header and properties values in following areas:

 

  • Xpath
  • Message Mapping
  • XSLT mapping

This will simpify the logic to be implemented.

 

  1. XPath:

 

Accessing header and property value in Xpath is pretty simple. Just use ${{headerName}} or ${{propertyName}} with the Xpath.

 

Consider below example:

 

XML :

Heaver Value

In order to collect all selling code having code type greater than 50, we can write the following XPATH:

/SELLING_CODE/PRODUCT/CODE[CODE_TYPE > $CodeType]

 

Same way property can be accessed in Xpath, if in case both header and property have same variable name then header will take preference.

 

  1. Message Mapping

 

To access header and property value in Message Mapping flow, you need to create two different script with extension *.gsh and place it in script folder of project.

 

getProperty.gsh

 

import com.sap.it.api.mapping.*;

import com.sap.it.api.mapping.MappingContext;

def String getProperty(String property_name, MappingContext context) {

    def propValue= context.getPropery(property_name);

    return propValue;

}

 

getHeader.gsh

 

import com.sap.it.api.mapping.*;

import com.sap.it.api.mapping.MappingContext;

def String getheader(String header_name, MappingContext context) {

    def headervalue= context.getHeader(header_name);

    return headervalue;

}

 

Add these scripts as custom function in Message Mapping.

 

Once these script are added as custom function, they can be used within mapping as shown below. Add the variable name of header or property you want to retrieve in the constant function.

 

 

 

  1. XSLT mapping

 

All parameters defined in the XLST mapping are automatically bound to Camel headers. In case a property or header with that name already exists, its value gets auto assigned to the parameter.

 

 

First the namespace where the extension functions (setheader, setProperty) is registered needs to be defined.

 

<xsl:stylesheet version=”2.0″
xmlns:xsl=http://www.w3.org/1999/XSL/Transform
xmlns:hci=http://sap.com/it/>

 

The exchange must be included in the XSLT as a parameter.

 

<xsl:param name=”exchange”/>

 

Setting a header or property:

When this is done, the extension functions can be called from within the XSLT sheet. The provided functions have three parameters (the first parameter must be the exchange, the other two are header name and header value or exchange property name and value (all strings)).

 

Here is an example:

<xsl:template match=”/”>

<xsl:value-of select=”hci:setHeader($exchange, ‘myname’, ‘myvalue’)”/>

<xsl:value-of select=”hci:setProperty($exchange, ‘myname’, ‘myvalue’)”/>

</xsl:template>

 

Getting the value of a header or property:

In order to get the value of a header/property that already exists, simply define a parameter with the same name.

 

Example: assume that a header/property with the name a1 already exists and has a value 123, simple define a parameter with the same name:

 

<xsl:param name= “a1”>

 

Now 123 is automatically assigned as a value to a1.

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Very informative blog.

       

      Author's profile photo Fasiullah S
      Fasiullah S

      Thanks Shashank. very helpful.

      One correction  - In getPropert.gsh, "def propValue= context.getPropery(property_name);" getProperty is misspelled as getPropery ("t" missing).

      Author's profile photo Yatanveer Singh
      Yatanveer Singh

      very useful, thanks for sharing.

      Yatan

      Author's profile photo Prasenjit Sarkar
      Prasenjit Sarkar

      The Property typo gets me each time 🙂

      Author's profile photo Allan Cunha
      Allan Cunha

      Very informative, thanks!

      Does anyone know a way to call a Groovy or JS custom script from within XSLT Mapping?

      Author's profile photo Philippe Addor
      Philippe Addor

      Thanks Shashank for this blog. It's worth mentioning that in order to read a parameter in XSLT one needs to prepend it with a $-sign.

      e.g.

      Assign it (without $):

      <xsl:param name="a1">

      then to read:

      <xsl:value-of select="$a1"/>
      Author's profile photo Keerthana Jayathran
      Keerthana Jayathran

      Hi Shashank Kesarwani ,

      I tried to call the property value in message mapping but the value is not getting updated.Could you please let me know what could be the issues.

      Thanks and Regards,
      Keerthana

      Author's profile photo Piotr Radzki
      Piotr Radzki

      I think the "context.getPropery(property_name);" code typo should never get fixed so we can train our brains all the time we go back to this very useful blog! 🙂

      BR, Piotr