0

I have a problem of adding record in UDT of SAP B1 in SDK.

ALL MY CODES

Public Class SystemForm
    Private oCompany As SAPbobsCOM.Company
    Private WithEvents SBO_Application As SAPbouiCOM.Application

    Private Sub SetApplication()

        Dim SboGuiApi As SAPbouiCOM.SboGuiApi
        Dim sConnectionString As String

        SboGuiApi = New SAPbouiCOM.SboGuiApi()
        sConnectionString = Command()
        SboGuiApi.Connect(sConnectionString)
        SBO_Application = SboGuiApi.GetApplication()
    End Sub

    Public Sub New()
        MyBase.New()

        SetApplication()

    End Sub


    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent

        If pVal.FormTypeEx = "UDO_FT_RPRL" AndAlso pVal.ActionSuccess = False AndAlso pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED AndAlso pVal.ItemUID = "2" AndAlso pVal.FormMode = 3 Then
            Dim oUsrTbl As SAPbobsCOM.UserTable
            Dim Res As Integer

            oCompany = New SAPbobsCOM.Company

            oUsrTbl = oCompany.UserTables.Item("@TODD")

            oUsrTbl.Code = "1"
            oUsrTbl.Name = "189"
            oUsrTbl.UserFields.Fields.Item("U_Amount").Value = 4000
            Res = oUsrTbl.Add()

            If Res = 0 Then
                SBO_Application.MessageBox("Added")
            Else
                SBO_Application.MessageBox("Error, failed to add Record")
            End If

        End If

    End Sub
End Class

I tried to do research but not help

Actually what I want to do is if I click on Add button of UDO then it updates my UDT called @TODD, but if I click Add button above codes bring the following error message " Addon 9000058 failed with exception; Event Type: 1".

Please anyone can help me

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Schadrack Rurangwa
  • 413
  • 12
  • 28
  • What is the value of oCompany.GetLastErrorCode and GetLastErrorDescription when Res<>0 ? – Daz Feb 19 '18 at 09:41
  • I tried to check oCompany.GetLastErrorCode but there is nothing displayed because if compile reaches to this line oUsrTbl = oCompany.UserTables.Item("@TODD") it fails where it displays error message on status bar of SAP B1 Addon 9000020 failed with exception; Event Type: 1. and @TODD is user defined table I created, I do know why it fails to that line. I declared Private oCompany As SAPbobsCOM.Company in Public class – Schadrack Rurangwa Feb 19 '18 at 10:29
  • I assumed the problem was during the save, but it's not. Please see my answer. – Daz Feb 19 '18 at 10:54

2 Answers2

1

You are missing a call to the Connect() method of DIAPI after creating the instance oCompany. Before calling this you need to set the connection context, either by specifying the server and login, or getting the session context from your UI-API connection. Assuming your UI-API object is called SBO_Application:

Dim Cookie as String = oCompany.GetContextCookie()
Dim conStr as String = SBO_Application.Company.GetConnectionContext(Cookie)
oCompany.SetSboLoginContext(conStr)
oCompany.Connect()

(untested code)

Obviously you'll probably want to check the Connect call succeeds before continuing.

Daz
  • 2,833
  • 2
  • 23
  • 30
  • Also, calling oCompany.Connect() in the event handler probably isn't a good idea. It will take a few seconds to connect which will hang-up the UI. The user might even think your addon has crashed. – Daz Feb 19 '18 at 10:59
  • Yes I connect with UI-API connection and my UI-API object is called SBO_Application, How can I connect it with oCompany? I think that if I connect with UI-API there is no other connection needed. the following is my normal connect Private Sub SetApplication() Dim SboGuiApi As SAPbouiCOM.SboGuiApi Dim sConnectionString As String SboGuiApi = New SAPbouiCOM.SboGuiApi() sConnectionString = Command() SboGuiApi.Connect(sConnectionString) SBO_Application = SboGuiApi.GetApplication() End Sub – Schadrack Rurangwa Feb 19 '18 at 11:24
  • You need both a UI-API connection (which you have, or your event wouldn't be handled) and a DI-API connection. The code I've given you will make a DI-API connection from an existing UI-API connection. The code in my answer should go directly after "oCompany = New SAPbobsCOM.Company". This will work, but putting this connection code in the event handler isn't the right place for it, as per my first comment above. – Daz Feb 19 '18 at 11:37
  • Thank you so much Daz!! you saved my time – Schadrack Rurangwa Feb 19 '18 at 12:35
0

This is the code I am using which connects and in my case sets up an 'oCompany' object that can be used for transactions and 'DoQuery' queries.

SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company();

oCompany.CompanyDB = "DATABASE_NAME";
oCompany.Server = "FTHANA01:30015";  // Your server name goes here
oCompany.LicenseServer = "FTHANA01:30000";
oCompany.SLDServer = "FTHANA01:40000";
oCompany.DbUserName = "SAPSYSTEM";
oCompany.DbPassword = psw1;
oCompany.UserName = "SAP Username";
oCompany.Password = psw2;

// We are using a SAP Hana database
oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_HANADB;
oCompany.UseTrusted = true;

int ret = oCompany.Connect();
string errMsg = oCompany.GetLastErrorDescription();
int ErrNo = oCompany.GetLastErrorCode();

if (ErrNo != 0)
    (we have an error)
else
    (success)

Database login credentials and user login credentials are both needed.

tonyb
  • 379
  • 2
  • 6