cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to call Function Module

Former Member
0 Kudos

Hello,

I am looking to create a User Defined Function in XI 3.0 that will call a function module that I have created. I need to send 4 input fields.

How can this be done?

Thanks,

Matt

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

You can manage this with the following code. Just adjust it a little to fit your function module.

function RFC_LOOKUP_API

Imports java.io.*;com.sap.aii.mapping.lookup.*;

String content = "";
MappingTrace importanttrace;
importanttrace = container.getTrace();
// filling the string with our RFC-XML (with values)
String m = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:BAPI_PO_GETDETAIL xmlns:ns0=\"urn:sap-com:document:sap:rfc:functions\"><HISTORY>X</HISTORY><PURCHASEORDER>";
m = m + a + "</PURCHASEORDER><SCHEDULES>X</SCHEDULES></ns0:BAPI_PO_GETDETAIL>";
RfcAccessor accessor = null;
ByteArrayOutputStream out = null;
try
{
// 1. Determine a channel (Business system, Communication channel)
Channel channel = LookupService.getChannel("BusinessSystem","RFCReceiver");
// 2. Get a RFC accessor for a channel.
accessor = LookupService.getRfcAccessor(channel);
// 3. Create a xml input stream representing the function module request message.
InputStream inputStream = new ByteArrayInputStream(m.getBytes());
// 4. Create xml payload
XmlPayload payload = LookupService.getXmlPayload(inputStream);
// 5. Execute lookup.
Payload result = accessor.call(payload);
InputStream in = result.getContent();
out = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
for (int read = in.read(buffer); read > 0; read = in.read(buffer)) {
out.write(buffer, 0, read);
}
content = out.toString();

}
catch(LookupException e)
{
importanttrace.addWarning("Error while lookup " + e.getMessage() );
}

catch(IOException e)
{
importanttrace.addWarning("Error " + e.getMessage() );
}
finally
{
if (out!=null) {
try {
out.close();
} catch (IOException e) {
importanttrace.addWarning("Error while closing stream " + e.getMessage() );
}
}
// 7. close the accessor in order to free resources.
if (accessor!=null) {
try {
accessor.close();
} catch (LookupException e) {
importanttrace.addWarning("Error while closing accessor " + e.getMessage() );
}
}
}

//returning the result u2013 RFC-XML.response
return content;

Answers (2)

Answers (2)

Former Member
0 Kudos

It worked! Thanks!

String content = "";
MappingTrace importanttrace;
importanttrace = container.getTrace();
// filling the string with our RFC-XML (with values)
String m = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:Z_UDF_CREATE_ZTOR_REC xmlns:ns0=\"urn:sap-com:document:sap:rfc:functions\">";
m = m + "<I_MESSAGEID>" + MsgID + "</I_MESSAGEID>";
m = m + "<I_EBELN>" + PONum + "</I_EBELN>";
m = m + "<I_ERDAT>" + MsgDate + "</I_ERDAT>";
m = m + "<I_UZEIT>" + MsgTime + "</I_UZEIT>";
m = m + "</ns0:Z_UDF_CREATE_ZTOR_REC>";
RfcAccessor accessor = null;
ByteArrayOutputStream out = null;
try
{
// 1. Determine a channel (Business system, Communication channel)
Channel channel = LookupService.getChannel("XI","GeneratedReceiverChannel_RFC");
// 2. Get a RFC accessor for a channel.
accessor = LookupService.getRfcAccessor(channel);
// 3. Create a xml input stream representing the function module request message.
InputStream inputStream = new ByteArrayInputStream(m.getBytes());
// 4. Create xml payload
XmlPayload payload = LookupService.getXmlPayload(inputStream);
// 5. Execute Record Create.
Payload result = accessor.call(payload);
InputStream in = result.getContent();
out = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
for (int read = in.read(buffer); read > 0; read = in.read(buffer)) {
out.write(buffer, 0, read);
}
content = out.toString();
 
}
catch(LookupException e)
{
importanttrace.addWarning("Error while writing " + e.getMessage() );
}
 
catch(IOException e)
{
importanttrace.addWarning("Error " + e.getMessage() );
}
finally
{
if (out!=null) {
try {
out.close();
} catch (IOException e) {
importanttrace.addWarning("Error while closing stream " + e.getMessage() );
}
}
// 7. close the accessor in order to free resources.
if (accessor!=null) {
try {
accessor.close();
} catch (LookupException e) {
importanttrace.addWarning("Error while closing accessor " + e.getMessage() );
}
}
}
 
//returning the result u2013 RFC-XML.response
return content;

Thanks,

Matt

Former Member
0 Kudos

Hi Matt,

Try to read the following:

1. Help info

http://help.sap.com/saphelp_nw04s/helpdata/en/d2/58cd3b11571962e10000000a11402f/frameset.htm

2. Really helpful blog on the topic by Alessandro Guarneri

/people/alessandro.guarneri/blog/2006/03/27/sap-xi-lookup-api-the-killer

Regards,

Dmitriy.