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.