cancel
Showing results for 
Search instead for 
Did you mean: 

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

0 Kudos

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?

Accepted Solutions (1)

Accepted Solutions (1)

former_member185682
Active Contributor
0 Kudos

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

0 Kudos

Thanks Diego.

It's true

Bye

Riccardo

Answers (0)