Skip to Content
Author's profile photo Rahul Yadav

FILE LOOKUP IN SAP PI USING UDF

This is my first post on SCN. We have few threads on File Lookup but i face few challenges when i was going through it. Thus i thought to create a simple step by step document for the same.

PI has two standard types of lookup available in ESR (i.e. JDBC and RFC lookup).  But we can achieve two more types of lookup in PI to fulfill our needs, those are File and SOAP lookup.

File Lookup is done when we want to map a value against an incoming payload value. In this scenario file may be maintained on some other server. We do a lookup to read the value against incoming field and map that value to the target payload.

To achieve this functionality we need to write a small UDF, which will leverage our requirement.

Step1: Create a UDF and import two packages as mentioned in screenshot.

           java.net.URL

           java.net.URLConnection

imp1.PNG

Step2: Copy paste below code in your PDF

(You need to replace username, password, server,path directory and filename in below code accordingly.)

String custId = “”;

try{

URL url =new URL(“ftp://<UserName>:<Password>@<IPAddress>/ <pathtodirectory>/ <filename.txt>”);

URLConnection con = url.openConnection();

InputStream lookupStream = con.getInputStream();

InputStreamReader reader = new InputStreamReader(lookupStream);

BufferedReader buffer = new BufferedReader(reader);

String read;

while((read=buffer.readLine()) != null){

String temp = read.substring(0,key.length());

if(key.equals(temp)){

custId = read.substring(key.length()+1,read.length());

if (  read.substring(key.length()+1,read.length()) != “00”){

int num = Integer.parseInt( read.substring(key.length()+1,read.length()));

num = num+2;

custId = Integer.toString(num);

}

}

}

  1. buffer.close();

} catch (Exception e){

          return e.toString();

}

return custId;

Step3: Create a file on external server which has input and output separated by “=”.

flkp1.JPG

Step4: We can verify our code through Display queue in message monitor’s tab by giving input in the source field.

Below screenshot shows if we are getting output as expected.

flkp2.JPG

This is the simplest way of doing a file lookup in SAP PI through UDF.


Note: File Lookup is not encouraged by SAP.


I hope you all like this post.!!!!!!

Assigned tags

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

      Good one.

      Author's profile photo Former Member
      Former Member

      Nice Article...thanks for sharing such useful information.

      Author's profile photo Bhavesh Kantilal
      Bhavesh Kantilal

      Hello Rahul,

      Within a UDF in PI, anything is possible. The idea of a RFC / JDBC / SOAP Lookup is that you use standard SAP API's that enable you to connect to the target system using a SAP PI Adapter. In your case what you have done is used a Java class to connect to an FTP Server, likewise due to the power of java, there can be multiple other things that can be done in the UDF but it should always be used with utmost caution. A comment that cautions users of this would be great so everybody is aware of the drawbacks of this approach.

      Regards,

      Bhavesh

      Author's profile photo Rahul Yadav
      Rahul Yadav
      Blog Post Author

      Thanks Sir for Your Guidance and Support from the beginning. 🙂 🙂

      Author's profile photo Former Member
      Former Member

      Hi,

      is a very good work this blog!

      I have a question....

      how can delete file with this method?

      I need delete files in one directory with udf on mapping...in the ftp server

      Author's profile photo Apu Das
      Apu Das

      Hi Rahul,

      Exactly same is mentioned below blog as well. But its better to avaoid file look up wherever possible.

      File Lookup in UDF - Process Integration - SCN Wiki

      Thanks,

      Author's profile photo Puneet Dhote
      Puneet Dhote

      Good One ....!!!