Skip to content

Instantly share code, notes, and snippets.

@viksingh
Forked from SriniBlog/getRFCLookupUDF.java
Created February 9, 2018 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viksingh/ea3cf967d071927f6f24b4152d371cdb to your computer and use it in GitHub Desktop.
Save viksingh/ea3cf967d071927f6f24b4152d371cdb to your computer and use it in GitHub Desktop.
RFC Lookup is simple UDF that can be used to execute RFC from UDF and fetch the output. This UDF uses RFC channel to make the calls to the SAP system.
public String getRFCLookupUDF(Container container) throws StreamTransformationException{
GlobalContainer globalContainer;
MappingTrace trace;
java.util.Map map;
trace = container.getTrace();
globalContainer = container.getGlobalContainer();
map = globalContainer.getTransformationParameters();
SystemAccessor rfcAccessor = null;
InputStream inputStream;
InputStream responseStream;
ByteArrayOutputStream out = null;
String rfcXMLMessage = new String();
String rfcContent = "";
try
{
//Lookup for the channel
Channel rfcChannel = LookupService.getChannel("BS_SAP_ECC_SYSTEM","CC_SAP_ECC_Lookup_RFC_Receiver");
rfcAccessor = LookupService.getSystemAccessor(rfcChannel);
rfcAccessor.setOperationName("Z_PDM_RFC_MATNR_EXISTCHECK"); //This is the Custom RFC Name
rfcAccessor.setOperationName("urn:sap-com:document:sap:rfc:functions"); //Namespace
//Prepare the RFC XML Message
rfcXMLMessage = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:Z_PDM_RFC_MATNR_EXISTCHECK xmlns:ns0=\"urn:sap-com:document:sap:rfc:functions\"><MATNR_EXISTCHECK>";
rfcXMLMessage = rfcXMLMessage + "<item><MATNR>" + MaterialNo + "</MATNR><WERKS>" + PlantCode + "</WERKS> <MATNR_EXIST/> <DEL_FLAG/></item>";
rfcXMLMessage = rfcXMLMessage + "</MATNR_EXISTCHECK></ns0:Z_PDM_RFC_MATNR_EXISTCHECK>";
//Execute the RFC
trace.addInfo("rfcXMLMessage: " + rfcXMLMessage);
inputStream = new ByteArrayInputStream(rfcXMLMessage.getBytes());
XmlPayload inPayload = LookupService.getXmlPayload(inputStream);
Payload outPayload = rfcAccessor.call(inPayload);
//Get the Output from the Lookup
trace.addInfo("Output from the RFC: " + outPayload.toString());
responseStream = outPayload.getContent();
trace.addInfo("responseStream: " + responseStream.toString());
out = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
for (int read =responseStream.read(buffer);read>0;read=responseStream.read(buffer))
{
out.write(buffer,0,read);
}
//Write the Output
rfcContent = out.toString();
trace.addInfo(rfcContent);
}catch(Exception e)
{
throw new Exception("Error in the RFCLookup." + e.getMessage());
}
finally
{
if (out!=null)
{
try
{
out.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to Close the instance of ByteArrayOutputStream." + e.getMessage());
}
}
if (rfcAccessor !=null)
{
try
{
rfcAccessor.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to close the rfcAccessor" + e.getMessage());
}
}
}//end of finally
return rfcContent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment