Skip to Content
avatar image
Former Member

How can I decide to Update or Add Item in UDO?

Hello.

I need to decide if operate an Add or an Update on an UDO

oCompanyService = oCompany.GetCompanyService();
oGeneralService = oCompanyService.GetGeneralService(MyService);
SAPbobsCOM.GeneralDataParams oGeneralParams = (SAPbobsCOM.GeneralDataParams)oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralDataParams);
oGeneralParams.SetProperty("Code", "Key");
oGeneralData = oGeneralService.GetByParams(oGeneralParams);
 
bool exist = true; // Need to modify
if (exist == true)
{
 oGeneralData.SetProperty("Name", descTable);
 oGeneralService.Update(oGeneralData);
}
else
{
 oGeneralData.SetProperty("Name", descTable);
 oGeneralService.Add(oGeneralData);
}

How can I decide if record exist and execute Update or Add?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Dec 13, 2016 at 03:51 PM

    Hi Riccardo,

    First, use the tag SAP Business One SDK as primary tag, with this I believe that you can get more answers.

    You can do a select in database using the RecordSet object, and check if your code exists, or use the method getByParams as you did.

    A sample with getByParams

    oGeneralParams = oSrvGeral.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralDataParams);
    oGeneralParams.SetProperty("Code", key);
    bool exist = false;
    try
    {
    	oGeneralData  = oGeneralService .GetByParams(oParamGeral);
    	if (!string.IsNullOrEmpty(oGeneralData .GetProperty("Code")))
    		exist = true;
    	else
    		oGeneralData  = oSrvGeral.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralData);
    }
    catch
    {
    	oGeneralData  = oSrvGeral.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralData);
    }
    

    Currently in my version, SAP B1 returns an exception when it does not find any register, then we need use the try catch block.

    Hope it helps,

    Kind Regards,

    Diego Lother

    Add comment
    10|10000 characters needed characters exceeded