cancel
Showing results for 
Search instead for 
Did you mean: 

Java syntax error : RFC Lookup

Former Member
0 Kudos

My UDF Source code has an syntax error: missing return statement }

but i can´t find the error.

any ideas ?


 //write your code here
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_GET_PRODH xmlns:ns0="urn:sap-com:document:sap:rfc:functions"><IN_MATNR>2100018693</IN_MATNR></ns0:Z_GET_PRODH>";

RfcAccessor accessor = null;
ByteArrayOutputStream out = null;

try
{

	// 1. Determine a channel (Business system, Communication channel) 
	Channel channel = LookupService.getChannel("xxxxxxxxxx","yyyyyyyy"); 

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

Message was edited by:

Gordon Breuer

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Gordon,

Guess you are using the code shown in Michal's article as it is. You need to parse the XML string and get the desired value you need and use return statement (if simple UDF) or resultlist.addvalue (if advanced UDF) to pass the output.

Regards,

Jai Shankar

Former Member
0 Kudos

yeah,

i allready found the problem, you´re right... the return ist missing.

How can i do that ? I need the return form the RFC call.

It´s a simple UDF !

Thx,

Gordon

Former Member
0 Kudos

You need to parse the XML first. The RFC response that is received now is in a XML format. You can either use a SAX/DOM parser and parse the XML to get the value you need and then finally you have to use return statement to pass the result of the UDF.

P.S: This return is different from the RFC response return value.

Sample code to parse the XL using DOM parser

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
documentInput = builder.parse(bais);
NodeList list = documentInput.getElementsByTagName("RESULT"); //Getting all RESULT tags in the node list
Node node = list.item(0); //Assuming only 1 RESULT is there in my RFC response
String strOutput = node.getFirstChild().getNodeValue(); //Get the value of the RESULT tag.

Regards,

Jai Shankar

Former Member
0 Kudos

Do i need a special import for the DOM ?

stefan_grube
Active Contributor
0 Kudos

I have a solution without DOM parser. The XML code is seen as a sting and the code looks for a specific tag. In my case it is OUTPUT, you can adjust the code to your tag.

String content = "";
MappingTrace importanttrace;
importanttrace = container.getTrace();
// filling the string with our RFC-XML (with values)
String m = "<rfc:ZZSG_TEST_XSLT_LOOKUP xmlns:rfc="urn:sap-com:document:sap:rfc:functions">"
                            + "<INPUT>" + request + "</INPUT></rfc:ZZSG_TEST_XSLT_LOOKUP>";
RfcAccessor accessor = null;
ByteArrayOutputStream out = null;
try
{
// 1. Determine a channel (Business system, Communication channel)
Channel channel = LookupService.getChannel("Airline_Group_Two","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 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 – RFC-XML.response
  int start = content.indexOf("<OUTPUT>") + 8;
  int end = content.indexOf("</OUTPUT>");
  if (end > start && start > 8)
    content = content.substring(start,end);
return content;
 

Former Member
0 Kudos

Great Job Stefan,

but my result is the same. I changed your OUTPUT to my OUT_PRODH but finally the result is a long xml string (not parsed)

any ideas ?

Former Member
0 Kudos

Stefan,

it´s working fine. I had a problem with an syntax check.

Thank You for your great Job.

- points rewarded -

stefan_grube
Active Contributor
0 Kudos

In fact I copied the code from here:

/people/sap.user72/blog/2005/07/15/copy-a-file-with-same-filename-using-xi

(last textbox)

There is a lot of information inside the SDN

Regards

Stefan

Answers (0)