Skip to end of metadata
Go to start of metadata


This post describes a user-defined mapping function for SAP XI that performs an RFC lookup using the SAP Mapping Lookup library in a scenario involving purchase orders.

This code builds on previously posted code examples on SDN, including an article by Danny De Roovere (

The code presented here relates specifically to a purchase order scenario involving the BAPI_PO_GETITEMS function module. The purpose of the lookup is to retrieve the value of an attribute of a specific order item (e.g., PRICE_UNIT), given the ID of the order (PURCHASEORDER) and the ID of the order item (PO_ITEM).

Besides serving as a reference for using the a RFC lookup using the SAP Mapping Lookup library, this example also provides some insights into how to parse the retrieved XML document for the desired information.

Function Signature

Inputs:             id of document containing list (e.g., PO Number), list item id value (PO item number)

Output:            value of list item attribute (e.g., PRICE_UNIT)


Below are the key steps for implementing this lookup function in a mapping

  1. Create the mapping
  2. Create new user-defined function (UDF) in mapping
  3. Provide input parameter information for the UDF
  4. Provide the libraries used in the code
  5. Paste the code
  6. Adjust the code so it runs in your environment and serves your purpose. This includes at least providing changing the SERVICE and CHANNEL_NAME. In addition a different result attribute (in our case PRICE_UNIT) or even an entirely different FUNCTION_MODULE may be specified. The latter may require further adjustments to the rfcXML string!
  7. Test the mapping


*  Purpose: RFC Lookup of document containing list (e.g., Purchase Order), returning value of list item with matching item id
*  Inputs: 1 - id document containing list (e.g., PO Number), 2 - list item id value (PO item number)
*  Output: value of list item (e.g., PRICE_UNIT)
*  Authors: Marc Haines, Innograte LLC & Kyle Fortney
*  created September, 2008
*  adapted from How to Lookup Data Via a RFC User Defined Function? - Thanks Danny De Roovere !
*  see

/* Define the following input parameters
   PO_Number (String)
   Item_Number (String)

/* Provide the following imports:*;java.util.Iterator;java.util.Map;*;javax.xml.parsers.*;org.w3c.dom.*;

// Enable tracing
AbstractTrace trace = container.getTrace();

// Set default values
final String SERVICE = "MY_SAP_SYSTEM", // Name of service defined in XI configuration
CHANNEL_NAME = "cc_rfc_receiver", // Name of communication channel defined for service
SAP_RFC_NAMESPACE = "urn:sap-com:document:sap:rfc:functions", // Namespace for SAP RFC definitions
FUNCTION_MODULE = "BAPI_PO_GETITEMS", // Name of the function module called
LIST_CONTENT_TAG = "PO_ITEMS", // Element that conatains the list items (e.g., PO_ITEMS contains multiple item elements)
LIST_ITEM_ID_TAG = "PO_ITEM", // Element of individual list item that contains the id of the item
LIST_VALUE_TAG = "PRICE_UNIT", // Element of the individual list item that conatains the value desired for output
VALUE_NOT_FOUND = ""; // Default return value in case something goes wrong

// Create document builder to create DOM XML document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;

try {
        // Create XML document using document builder
        builder = factory.newDocumentBuilder();
} catch (Exception e) {
        trace.addWarning("Error creating DocumentBuilder - " +
        return null;

// Define XML for RFC Request
String rfcXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:" +
        FUNCTION_MODULE + " xmlns:ns0=\"" + SAP_RFC_NAMESPACE +
        FUNCTION_MODULE + ">";

// Prepare and perform RFC Lookup ...
RfcAccessor accessor;
Payload result;

try {
        //  Determine a communication channel (Business system + Communication channel)
        Channel channel = LookupService.getChannel(SERVICE, CHANNEL_NAME);

        //  Get a RFC accessor for the channel.
        accessor = LookupService.getRfcAccessor(channel);

        //  Create an XML input stream that represents the RFC request message.
        InputStream is = new ByteArrayInputStream(rfcXML.getBytes());

        //  Create the XML Payload
        XmlPayload payload = LookupService.getXmlPayload(is);

        //  Execute the lookup.
        result =;

        if (result == null) {
                trace.addWarning("result of RFC call is null");

} catch (LookupException e) {
        trace.addWarning("Error during lookup - " + e);

// Parsing RFC Response Document
Document docResponse = null;
InputStream in = result.getContent();
String returnValue = VALUE_NOT_FOUND;
NodeList poItems = null;

try {
        docResponse = builder.parse(in);
        if (docResponse == null) {
                trace.addWarning("docResponse is null");
        // Get the list of PO items from RFC structure (item nodes, child nodes of PO_ITEMS)
        // Important: getElementsByTagName returns a *node list*.
        //            So the first - even if there is only one - item needs to be picked - item(0)!
        //            Only when having the PO_ITEMS *node* the getChildNodes() function can be used to get the PO items.
        poItems = docResponse.getElementsByTagName(

        // Iterate over PO items (item nodes in RFC structure) until item with right item number is found
        for (int i = 0; i < poItems.getLength(); i++) {
                Element e = (Element) poItems.item(i);
                // Get the PO_ITEM value for the current item
                String poItem =

                // Test if value of PO_ITEM from current node matches input parameter Item_Number
                if (poItem.equals(Item_Number)) {
                        // Assign text value PRICE_UNITof desired tag to result
                        returnValue = e.getElementsByTagName(LIST_VALUE_TAG).item(
catch (Exception e) {
        trace.addWarning("Error when parsing RFC Response - " + e.getMessage());

try {

        // Free resources, close the accessor..
        if (accessor != null) {
                try {
                } catch (LookupException e) {
                        trace.addWarning( "Error while closing accessor " + e.getMessage());
} catch (Exception e) {
        trace.addWarning("Result value not found in DOM - " + e);

// return the result obtained above
return returnValue;