cancel
Showing results for 
Search instead for 
Did you mean: 

How can I convert this UDF to be Dynamic?

Former Member
0 Kudos

Hi there I have this UDF that calls a RFC, currently you can specify which table you want to read from and some input parameters. Could someone help me modify this so that I can specify which RFC to call? It only has to have 1 input parameter so not to worry about that. I am trying to change it to another RFC but it does not work. Heres the original UDF calling a static RFC with any table:

// declare parameters

String returnValue = "";

String rfcXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <ns0:RFC_READ_TABLE xmlns:ns0=\"urn:sap-com:document:sap:rfcfunctions\"><QUERY_TABLE>ZMAPPING_TABLE</QUERY_TABLE><DATA></DATA><FIELDS></FIELDS><OPTIONS><item><TEXT>EAN_UPC = '"EAN_UPC"'</TEXT></item></OPTIONS></ns0:RFC_READ_TABLE>";

AbstractTrace trace = container.getTrace();

RfcAccessor accessor = null;

ByteArrayOutputStream out = null;

try

{

// 1. Determine a communication channel (Business system + Communication channel)

Channel channel = LookupService.getChannel(businessSystem,communicationChannel);

// 2. Get a RFC accessor for the channel.

accessor = LookupService.getRfcAccessor(channel);

// 3. Create an XML input stream that represents the RFC request message.

InputStream inputStream = new ByteArrayInputStream(rfcXML.getBytes());

// 4. Create the XML Payload

XmlPayload payload = LookupService.getXmlPayload(inputStream);

// 5. Execute the lookup.

Payload result = null;

result = accessor.call(payload);

InputStream in = result.getContent();

// 6. Create a DOM structure from the input XML

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(in);

NodeList list = document.getElementsByTagName("WA"); // The lookupValue is available as WA tag in the response

Node node = list.item(0);

if (node != null) {

node = node.getFirstChild();

if (node != null) {

returnValue = node.getNodeValue();

}

}

// 7. To free resources, close the accessor..

if (accessor!=null) {

try {

accessor.close();

} catch (LookupException e) {

trace.addWarning("Error while closing accessor " + e.getMessage() );

}

}

} catch (Exception e) {

trace.addWarning("Error" + e);

}

// 8. return a single id value to the message mapping

return returnValue;

And here is the one I have dynamically calling a RFC:

// declare parameters

String returnValue = "";

String rfcXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <ns0:" + RFC_NAME + " xmlns:ns0=\"urn:sap-com:document:sap:rfcfunctions\"><INPUT_PAR>Input_Value</INPUT_PAR></ns0:" + RFC_NAME + ">";

AbstractTrace trace = container.getTrace();

RfcAccessor accessor = null;

ByteArrayOutputStream out = null;

try

{

// 1. Determine a communication channel (Business system + Communication channel)

Channel channel = LookupService.getChannel(businessSystem,communicationChannel);

// 2. Get a RFC accessor for the channel.

accessor = LookupService.getRfcAccessor(channel);

// 3. Create an XML input stream that represents the RFC request message.

InputStream inputStream = new ByteArrayInputStream(rfcXML.getBytes());

// 4. Create the XML Payload

XmlPayload payload = LookupService.getXmlPayload(inputStream);

// 5. Execute the lookup.

Payload result = null;

result = accessor.call(payload);

InputStream in = result.getContent();

// 6. Create a DOM structure from the input XML

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(in);

NodeList list = document.getElementsByTagName("WA"); // The lookupValue is available as WA tag in the response

Node node = list.item(0);

if (node != null) {

node = node.getFirstChild();

if (node != null) {

returnValue = node.getNodeValue();

}

}

// 7. To free resources, close the accessor..

if (accessor!=null) {

try {

accessor.close();

} catch (LookupException e) {

trace.addWarning("Error while closing accessor " + e.getMessage() );

}

}

} catch (Exception e) {

trace.addWarning("Error" + e);

}

// 8. return a single id value to the message mapping

return returnValue;

Thanks,

Jan

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi, Thanks for all the help. I fixed the issue. It was thant WA, I just needed to specify the correct return parameter.

Thanks,

Jan

former_member206760
Active Contributor
0 Kudos

Dear Jan,

Wonder why do you want the UDF to call any RFC as you must be knowing beforehand only which RFC needs to be called.

Anyways if you want you can also pass RFC name in the source message or in the graphical mapping part before UDF and then use a variable inside UDF that will utilise that RFC name .

I hope this serves your purpose

Former Member
0 Kudos

Thats what I'm trying but it just don't return any results. For the 2nd RFC I use the exact same code, all I'm changing is the XML structure passing the input parameters. For the 1st RFC I have:

String rfcXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <ns0:RFC_READ_TABLE xmlns:ns0=\"urn:sap-com:document:sap:rfcfunctions\"><QUERY_TABLE>ZMAPPING_TABLE</QUERY_TABLE><DATA></DATA><FIELDS></FIELDS><OPTIONS><item><TEXT>EAN_UPC = '"EAN_UPC"'</TEXT></item></OPTIONS></ns0:RFC_READ_TABLE>";

The rfc is RFC_READ_TABLE and it has got these parameters QUERY_TABLE, DATA, FIELDS, OPTIONS & TEXT.. So My 2nd RFC I have

String rfcXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <ns0:SAP_TO_ISO_MEASURE_UNIT_CODE xmlns:ns0=\"urn:sap-com:document:sap:rfcfunctions\"><SAP_CODE>PAL</SAP_CODE></ns0:SAP_TO_ISO_MEASURE_UNIT_CODE>";

This RFC is, SAP_TO_ISO_MEASURE_UNIT_CODE and only has 1 input parameter SAP_CODE. obviously I'll change the "PAL" to be dynamic but the results are initial..

former_member181985
Active Contributor
0 Kudos

Hi,

Obviously the second dynamic code doesnt work, since you have to provide the dynamic request as expected by the dynamic RFC and you to parse Repsonse structure dynamically.

NodeList list = document.getElementsByTagName("WA"); // The lookupValue is available as WA tag in the response

The tag WA will/might not exist for other BAPI's............

Thanks,

- Gujjeti.

Former Member
0 Kudos

hmm.. not sure how that 2nd part works. Is there maybe then some blog showing how to call a RFC? I've been going over a few of them but with no luck getting them to work.

former_member181962
Active Contributor
0 Kudos

Hi Jan,

When you say you want the UDF to be dynamic, do you mean it should work for any RFC name that you want to pass as parameter?

Or do you want the UDF to work for any TABLE that you want to pass as parameter?

Regards,

Ravi

Former Member
0 Kudos

It must be able to call any RFC, for the parameters only 1 is needed but if it could work with an array passing all the values it would be awesome. I tried some stuff I found on sdn but nothing works so far.

Thanks,

Jan

former_member181985
Active Contributor
0 Kudos

why you require this kind of behavior? Any specific requirement.

Former Member
0 Kudos

Well I have 3 RFC's I need to call in my scenario and of the 3 only 1 of them works so far. So If I can specify the frc and the fields required by the rfc it would make things allot easier. Like for the 2nd example I gave, I have the rfc which only needs 1 input parameter, I made the changes in the input XML but apparently the 2nd part of the code is not working.

former_member181985
Active Contributor
0 Kudos

Hi Jan,

What error you are getting for the second UDF code......?

Test the code from Message Mapping "Test Tab"............and lets know the error.

I suspect that the second RFC (which is failing) might not have the tag "WA" in its response structure.........

Former Member
0 Kudos

The 2nd RFC does not produce any error or results. Show successful but with no output data. If there is no "WA" in its response structure, how would I fix this or work around it?

Edited by: Jan de Lange on Mar 16, 2009 12:48 PM

former_member181985
Active Contributor
0 Kudos

1)You can check the Second RFC Response Structure using SE37.

Check export structure using Display code.

2)Second way.......

Change the code here

if (node != null) {
returnValue = node.getNodeValue();
}

if (node != null) {
returnValue = node.getNodeValue();
}else
{
returnValue = "No WA tag in response";
}

Now execute the mapping using Test Tab. Now you should see "No WA tag in response" in the target structure for the mapped field.

Edited by: Praveen Gujjeti on Mar 16, 2009 5:26 PM