Skip to Content
avatar image
Former Member

JCO sap b1 how to pass parameters to GeneralService

Hallo,

I'm developing an application with JCO java connetor for sap b1. I need to load an UDO, but I don't know how to set my parameters on the general service, in order to load a sepcific record.

On the help center I found this example in VB:

sCmp = oCompany.GetCompanyService

'Get a handle to the SM_MOR UDO
oGeneralService = sCmp.GetGeneralService("SM_MOR")

'Get UDO record
oGeneralParams = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralDataParams)

oGeneralParams.SetProperty("DocEntry", "2")
oGeneralData = oGeneralService.GetByParams(oGeneralParams) 

So I tried to write the same code in java:

IGeneralService generalService = company.getCompanyService().getGeneralService("MY_UDO");
IGeneralDataParams generalParams = (IGeneralDataParams)generalService.getDataInterface(SBOCOMConstants.GeneralServiceDataInterfaces_gsGeneralDataParams); 
... 

but it gives me a class cast exception:

ClassCastException: Cannot cast com.sap.smb.sbo.wrapper.com.Dispatch to com.sap.smb.sbo.api.IGeneralDataParams

So my question is, how can I build an IGeneralDataParams, in order to pass parameters to generalService.getByParams(...)?

Thanks

Giu

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Nov 09, 2016 at 03:27 PM

    Hi Giu,

    I usually work with SDK and C#, and I'm not familiarize with JCO, but did you try run your code without do the cast?

    GeneralDataParams generalParams = generalService.getDataInterface(SBOCOMConstants.GeneralServiceDataInterfaces_gsGeneralDataParams);
    

    Kind Regards,

    Diego Lother

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 09, 2016 at 04:18 PM

    Hi Diego,

    the problem is that

    generalService.getDataInterface(SBOCOMConstants.GeneralServiceDataInterfaces_gsGeneralDataParams);

    returns an Object type, not a GeneralDataParams type.

    Giu

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 10, 2016 at 09:11 AM

    I tried to do something like this:

    IGeneralService generalService = company.getCompanyService().getGeneralService("MY_UDO");
    Object dataInterface = generalService.getDataInterface(SBOCOMConstants.GeneralServiceDataInterfaces_gsGeneralDataParams);
    GeneralDataParams gp = new GeneralDataParams(dataInterface);
    gp.setProperty("DocEntry", "1");
    IGeneralData data = generalService.getByParams(gp); 

    But if I call

    data.getProperty("DocEntry") 

    it gives me this error:

    com.sap.smb.sbo.wrapper.com.ComFailException: VariantChangeType failed

    I also tried to call the method

    data.toXmlString() 

    and it seems to work correctly. It gives me an xml populated with expected values.

    So I don't get why data.getProperty("DocEntry") gives error.

    Ideas?

    Giu

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 10, 2016 at 05:17 PM

    Ok, it's not really clear to me but I think it's a library bug... or meabe I miss something. I decompiled GeneralData.getProperty and this is the code:

    public Object getProperty(String paramString){     
      Variant localVariant = Dispatch.call(this.m_GeneralData, "GetProperty", paramString);     
      return localVariant.getObject();
    } 

    The error is thrown when is called the native method

    localVariant.getObject();

    I noted that Variant has also other methods to convert the object in a specific type:

    getBoolean(), getString(), getInt() etc...

    So I tried to replicate the same code using the specific convertion method like this:

    Variant myVariant = Dispatch.call(data.retrieveRawGeneralData(), "GetProperty", "DocKey");  
    myVariant.getInt(); 

    And I noted that using these methods it works!!!

    So, I don't know if this is the right solution, but it works. The only annoing thing is that I have to choose the right method to call according to the expected data type.

    P.S.

    It would be very nice to have a documentation with some code example for JCO sdk from SAP.

    SAP Business One SDK Help Center is usefull, but java is not vb. I know this library is just a wrapper but sometimes a more specific documentation would be better.

    Giu

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Giu,

      If you have installed the Sap Business One SDK on your machine, inside of installation folder/Help/ there is a zip file called JCO. Unzip this and you see the documentation. Unfortunately there is no sample code in this documentation.

      Hope it helps.

      Kind Regards,

      Diego Lother

      img5.png (28.4 kB)
      img6.png (112.9 kB)