0

`Hello All, I am trying to create a table in the SAP B1. But I am getting an error code "-1120". For creating a table I have fetched a data from the json file. And by using that data I tried to create the tables in the SAP B1. But I am getting an error code "-1120".

Here is the code

            string text = File.ReadAllText(@"C:\Users\vijay\Documents\Visual Studio 2015\Projects\TestAddon_03\TestAddon_03\json1.json");
            var tableInfo = JsonConvert.DeserializeObject<TableInfo>(text);

            try
            {
                foreach (var table in tableInfo.tables)
                {
                    string tbName = table.tableName;
                    string tbDis = table.tableDescription;
                    string tbType = table.tableType;

                    GC.Collect();
                    SAPbobsCOM.UserTablesMD objUserTableMD = (SAPbobsCOM.UserTablesMD)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserTables);
                    objUserTableMD.TableName = tbName;
                    objUserTableMD.TableDescription = tbDis;

                    switch (tbType)
                    {
                        case "Document":
                            objUserTableMD.TableType = SAPbobsCOM.BoUTBTableType.bott_Document;
                            break;
                        case "MasterData":
                            objUserTableMD.TableType = SAPbobsCOM.BoUTBTableType.bott_MasterData;
                            break;
                        case "NoObject":
                            objUserTableMD.TableType = SAPbobsCOM.BoUTBTableType.bott_NoObject;
                            break;
                        default:
                            Application.SBO_Application.MessageBox("Invalid Table Type");
                            break;
                    }

                    int result = objUserTableMD.Add();

                    //Application.SBO_Application.MessageBox(Convert.ToString(result));

                    if(result != 0)
                    {
                        Application.SBO_Application.MessageBox("Table Not Created" + "\n --------- \n" + oCompany.GetLastErrorDescription() );
                    }
                    else
                    {
                        Application.SBO_Application.MessageBox("Table Created");
                        foreach (var field in table.fields)
                        {
                            string fieldNm = field.fieldName;
                            string fieldType = field.fieldType;
                            int fieldSize = field.fieldSize;

                            SAPbobsCOM.UserFieldsMD objUserFieldsMD = (SAPbobsCOM.UserFieldsMD)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
                            objUserFieldsMD = null;
                            objUserFieldsMD.TableName = tbName;
                            objUserFieldsMD.Name = field.fieldName;

                            switch (fieldType)
                            {
                                case "Numeric":
                                    objUserFieldsMD.Type = SAPbobsCOM.BoFieldTypes.db_Numeric;
                                    break;
                                case "String":
                                    objUserFieldsMD.Type = SAPbobsCOM.BoFieldTypes.db_Alpha;
                                    break;
                                case "Date":
                                    objUserFieldsMD.Type = SAPbobsCOM.BoFieldTypes.db_Date;
                                    break;
                                case "Float":
                                    objUserFieldsMD.Type = SAPbobsCOM.BoFieldTypes.db_Float;
                                    break;
                                case "Memo":
                                    objUserFieldsMD.Type = SAPbobsCOM.BoFieldTypes.db_Memo;
                                    break;
                                default:
                                    Application.SBO_Application.MessageBox("Invalid FieldType");
                                    break;
                            }

                            objUserFieldsMD.Size = field.fieldSize;
                            int fieldResult = objUserFieldsMD.Add();

                            if (fieldResult != 0)
                            {
                                Application.SBO_Application.MessageBox("Fields Not Created");
                            }
                            else
                            {
                                Application.SBO_Application.MessageBox("Fields Created");
                            }
                        }

                    

                       // Application.SBO_Application.MessageBox(tbName + " " + tbDis + " " + tbType + " " + fieldNm + " " + fieldType + " " + fieldSize);
                    }
                }
            }
            catch(Exception Create)
            {
                Application.SBO_Application.MessageBox(Create.ToString());
            }

And This is a sample json file

{
    "tables": [
        {
            "tableName": "SAMP1",
            "tableDescription": "SampleTable 1",
            "tableType": "Document",
            "fields": [
                {
                    "fieldName": "CF1",
                    "fieldType": "String",
                    "fieldSize": 100
                }
            ]
        },
        {
            "tableName": "SAPM2",
            "tableDescription": "SampleTable 2",
            "tableType": "MasterData",
            "fields": [
                {
                    "fieldName": "CF2",
                    "fieldType": "Numeric",
                    "fieldSize": 8
                },
                {
                    "fieldName": "CF3",
                    "fieldType": "String",
                    "fieldSize": 50
                }
            ]
        }
    ]
}

I am getting the error at the below line:

Application.SBO_Application.MessageBox("Table Not Created" + "\n --------- \n" + oCompany.GetLastErrorDescription() );

Error:

SAPbobsCOM.ICompany.GetLastErrorDescription returend "Ref Count for this object is higher then 0".

1 Answers1

0

You need to set objUserTableMD to null and do another garbage collection after creating the table and before adding its fields. You might also need to call Marshal.ReleaseComObject() on the reference.

B1 only allows a single instance of schema modifying objects to exist.

Daz
  • 2,833
  • 2
  • 23
  • 30
  • Hello Daz, Thank you for the answer. I tried to set the objUserTableMD to 'null' but I am getting an error like -> Object reference not set to an instance of an object. – CoolMaster Jan 27 '23 at 06:27
  • Do you have any other method to creating the table in the SAP B1. Can you please share me the code. – CoolMaster Jan 27 '23 at 07:40
  • You have another error in the code. After creating the table you have a objUserFieldsMD = null between reinitialising the object and using it. – Daz Jan 28 '23 at 09:47