I use SAPJCo3.jar
for Java to SAP connection and communication but when execute the program, get error at JcoStructure
part. The debugger pass successfully to function module connection part I see at debugger, but in this line I get interesting error :
JCoStructure returnStructure = function.getExportParameterList().getStructure("ET_CUSTOMERS");
and I get this exception when run to the program :
Exception in thread "main" com.sap.conn.jco.ConversionException: (122) JCO_ERROR_CONVERSION: Cannot convert field ET_CUSTOMERS of type TABLE to StructureRecord at com.sap.conn.jco.rt.AbstractRecord.createConversionException(AbstractRecord.java:416) at com.sap.conn.jco.rt.AbstractRecord.createConversionException(AbstractRecord.java:411) at com.sap.conn.jco.rt.AbstractRecord.getStructure(AbstractRecord.java:2585) at com.sap.conn.jco.rt.AbstractRecord.getStructure(AbstractRecord.java:3060) at com.sap.conn.jco.rt.AbstractRecord.getStructure(AbstractRecord.java:53) at SAP.GetCustomers.step4WorkWithTable(GetCustomers.java:100) at SAP.Main.main(Main.java:13)
That is the complete code of my function :
public static void step4WorkWithTable() throws JCoException
{
JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);
JCoFunction function = destination.getRepository().getFunction("Z_SYCLO1_GET_CUSTOMERS");
if(function == null)
throw new RuntimeException("Z_SYCLO1_GET_CUSTOMERS not found in SAP.");
JCoStructure returnStructure = function.getExportParameterList().getStructure("ET_CUSTOMERS");
if (! (returnStructure.getString("TYPE").equals("")||returnStructure.getString("TYPE").equals("S")))
{
throw new RuntimeException(returnStructure.getString("MESSAGE"));
}
JCoTable codes = function.getTableParameterList().getTable("Z_SYCLO1_CUSTOMERS_LIST_TT");
for (int i = 0; i < codes.getNumRows(); i++)
{
codes.setRow(i);
System.out.println(codes.getString("KUNNR") + '\t' +
codes.getString("NAME1") + '\t' +
codes.getString("NAME2") + '\t' +
codes.getString("TELF1") + '\t' +
codes.getString("STRAS") + '\t' +
codes.getString("ORT01") + '\t' +
codes.getString("REGIO") + '\t' +
codes.getString("PSTLZ") + '\t' +
codes.getString("LAND1"));
}
codes.firstRow();
for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow())
{
function = destination.getRepository().getFunction("Z_SYCLO1_GET_CUSTOMERS");
if (function == null)
throw new RuntimeException("Z_SYCLO1_GET_CUSTOMERS not found in SAP.");
function.getImportParameterList().setValue("IV_USERNAME", codes.getString("UOZKER"));
function.getExportParameterList().setActive("ET_CUSTOMERS",false);
try
{
function.execute(destination);
}
catch (AbapException e)
{
System.out.println(e.toString());
return;
}
returnStructure = function.getExportParameterList().getStructure("ET_CUSTOMERS");
if (! (returnStructure.getString("TYPE").equals("") ||
returnStructure.getString("TYPE").equals("S") ||
returnStructure.getString("TYPE").equals("W")) )
{
throw new RuntimeException(returnStructure.getString("MESSAGE"));
}
JCoStructure detail = function.getExportParameterList().getStructure("ET_CUSTOMERS");
System.out.println(codes.getString("KUNNR") + '\t' +
codes.getString("NAME1") + '\t' +
codes.getString("NAME2") + '\t' +
codes.getString("TELF1") + '\t' +
codes.getString("STRAS") + '\t' +
codes.getString("ORT01") + '\t' +
codes.getString("REGIO") + '\t' +
codes.getString("PSTLZ") + '\t' +
codes.getString("LAND1"));
}
The naming ET_
suggests that ET_CUSTOMERS
is a table. You can't treat a table like a structure - you have to obtain a table reference first, then either navigate to an existing row or create a new one, and maybe then you can treat that row like a structure. Please also see this answer which has a similar background.
("Maybe" because tables may also be created based on unstructured types, which leads to really weird situation when combined with the JCo, so you should avoid this.)