Wednesday, 9 March 2016

Using File Lookup in SAP PI/PO

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



Step2: Copy paste below code in your PDF

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);
}
}
}

buffer.close();

} catch (Exception e){
return e.toString();
}

return custId;

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



Step4: We can verify our code through Display queue by giving input in the source field.
Below screenshot shows we are getting output as expected.



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



6 comments:

  1. Thanks for this great share. This site is a fantastic resource. Keep up the great work. Here i found how to Using File Lookup in SAP PI/PO. Learn more about sap po course by sap experts with live projects.
    sap po training

    ReplyDelete
  2. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor led live training in SAP PI , kindly Contact GRONYSA
    Click for SAP PI Course details SAP PI
    GRONYSA Offer World Class Virtual Instructor led training on SAP PI. We have industry expert trainer. We provide Training Material and Software Support. GRONYSA has successfully conducted 10,000 + trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Nitesh Kumar
    GRONYSA
    E-mail: nitesh.kumar@gronysa.com
    Ph: +91-9632072659/ +1-2142700660
    www.GRONYSA.com


    ReplyDelete
  3. Thank you rahul yadav, i am really happy with this explanation about sap pi/po.
    sap pipo training in hyderabad

    ReplyDelete
  4. Hi Rahul,

    Can we read the file for NFS location? If yes, what code modifications will be required?
    BR,
    Rashmi

    ReplyDelete
  5. Hi Rahul,

    Can we read the file for NFS location? If yes, what code modifications will be required?
    BR,
    Rashmi

    ReplyDelete