cancel
Showing results for 
Search instead for 
Did you mean: 

RefCount Error When Adding UserField Using SAP Business One SDK

JonathasCosta
Newcomer
0 Kudos
280

 

Hello SAP Community,

I'm facing an issue with the SAP Business One SDK while trying to add a UserField to a table. The error seems related to the reference count of COM objects. Here’s the context:

I have a method that retrieves field information from a source table using a UserFieldsMD object and then attempts to create a similar field in a destination table. I'm using Recordset and UserFieldsMD objects to handle this process.

Here’s a high-level outline of my process:

  1. I initialize a UserFieldsMD object and a Recordset to retrieve the field data from the source table.
  2. I extract the necessary field data and store it in a .NET dictionary.
  3. I release the UserFieldsMD and Recordset COM objects to manage resources effectively.
  4. I initialize a new UserFieldsMD object for the destination table and set its properties based on the extracted data.

When I attempt to add this UserField to the destination table, I encounter a refcount error.

I suspect that this issue is related to how COM objects are managed and released within the context of my method, especially since there appears to be a limitation on the number of active COM objects.

Could someone please provide guidance on best practices for managing COM object reference counts when adding UserField objects in SAP Business One SDK? Are there specific patterns or techniques recommended to avoid such errors?

Here is the full code I am using. The error occurs at the CreateField method (line 255):
https://gist.github.com/jonathascosta/6ea59e966c46eb43a74992151f60ab85

Any insights or suggestions on how to properly handle the lifecycle of these COM objects to avoid refcount errors would be greatly appreciated.

Thank you in advance for your assistance!

 

 

View Entire Topic
ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi JonathasCosta,

To avoid this error please ensure that no other object are running during the addition of the user defined fields. When you are trying to update table/field in the database, you should kill all the running objects and use only the object that the update. You should also set the User Defined Table/User Defined Field to be equal to nothing at the end of the procedure.

In case of DIAPI

 

oUFields = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)

'Add the field code .....................

'IMPORTANT: Release the object
System.Runtime.InteropServices.Marshal.ReleaseComObject(oUFields)

'Then set object variable to Nothing
oUFields = Nothing

 

Kind regards,

ANKIT CHAUHAN

SAP Business One Support