Skip to Content
Technical Articles
Author's profile photo Pavan Kumar Pothula

REST Look Up using UDF in SAP PO

Hello Experts,

I have read many blog posts for performing the REST Look up using Java mapping but I felt it would be helpful if we can achieve REST look up using UDF. Hence I have tried blogging one of the scenario where I have used REST Look up using UDF.

 

Scenario: We have a scenario where a field ‘Code’ value has to be checked in the target system for its existence.ย  Based on its existence I have to pass the value “True” if it exits else I have to pass “False” in my UDF.

 

Step1: Create a receiver communication channel for your look up using REST adapter.

Communication Channel : CC_LookupChannel_REST

Create a Message type to receive your response message through the lookup channel and assign the message type in the Element name and its namespace respectively.

Step2: Create your Message mapping as required and pass the required source fields to the UDF as inputs. In my case I have passed a value ID and Bussiness Component and Lookup Channel Names as inputs to the UDF.

Step3: Create the UDF in your message mapping using the below code.

Here I am passing business component and Lookup channels as inputs which are passed using parameterized mapping.

 

Parameterized Mapping Values:

Following Parameterized values are to be passed from your ICO/IFLOW.

Lookup Channel Name(ChannelName): CC_LookupChannel_REST

Business Component: BC_Target

 

Code:

public void RESTLookup(String[] var1, String[] BusinessComponent, String[] ChannelName, ResultList result, Container container) throws StreamTransformationException{

 

AbstractTrace trace = container.getTrace();
String floc=””;
try{

Channel channel= LookupService.getChannel(BusinessComponent[0],ChannelName[0]);

SystemAccessor accessor = LookupService. getSystemAccessor(channel);

// Look up service xml

String RestXML = “<?xml version=\”1.0\” encoding=\”UTF-8\”?><ns2:MT_RESTIN_V01 xmlns:ns2=\”urn:std:restlookup”><Equipment><Code>”+var1[0]+”</Code></Equipment></ns2:MT_RESTIN_V01>”;

InputStream inputStream= new ByteArrayInputStream(RestXML.getBytes());

XmlPayload payload = LookupService.getXmlPayload(inputStream);

Payload RESTOutPayload = null;

RESTOutPayload = accessor.call(payload);

InputStream inps = RESTOutPayload.getContent();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inps);

NodeList list = document.getElementsByTagName(“Code”);
Node node = list.item(0);
if (node != null) {
//result.addValue( “True”);
//floc = node.getNodeValue();
node = node.getFirstChild();
if (node != null) {
floc = node.getNodeValue();
//result.addValue(floc);

}
}

}catch (Exception e) {
}

if(floc.equals(var1[0]))
{
result.addValue( “True”);
}
else
{
result.addValue( “False”);
}

}

 

Hope this will be helpful ๐Ÿ™‚

Thanks,

Pavan Kumar P

Reference Blog posts:

https://blogs.sap.com/2015/09/11/yes-rest-lookup-is-possible-in-pi/

https://blogs.sap.com/2013/08/07/how-to-soap-lookup/

 

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Bhargava Krishna Talasila
      Bhargava Krishna Talasila

      Nice blog.ย  Thanks

      Author's profile photo Pavan Kumar Pothula
      Pavan Kumar Pothula
      Blog Post Author

      Thanks Bhargava Krishna.

       

      Author's profile photo Yunze Wang
      Yunze Wang

      Hi Pavan Kumar,

      I use ASMA instead ofย  xpath substitution,I set ASMA in UDF before call lookup channel,but when using it in channel,the channel didn't get the ASMA values,but it did showup in the payload of dynamic configuration,how come?

      Kind Regards

      yunze

      Author's profile photo Imran Shaik
      Imran Shaik

      Hi Pavan,

      I am getting below error while activating the mapping. I am using your code for RESTLookUp to get the customer information from thirdparty system which is based on TMF622 format.

      We are using KaTe Restful adapter in receiver channel.

       

      what could be the reason?

       

      Thanks In Advance

      Regards,

      Imran Shaik

       

      Author's profile photo Evgeniy Kolmakov
      Evgeniy Kolmakov

      Hi!

      As it stands from error message and your code snippet you use wrong double quote character:

      
      
      Wrong: String RestXML = โ€œ<?xml version=\โ€1.0\โ€ encoding=\โ€UTF-8\โ€?>โ€;
      
      
      Right: String RestXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
      Regards, Evgeniy.