Skip to Content
0

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

Dec 13, 2016 at 03:15 PM

76

avatar image

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?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
DIEGO LOTHER Dec 13, 2016 at 03:51 PM
0

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thanks Diego.

It's true

Bye

Riccardo

0