Search code examples
visual-studiosdksapb1sap-business-one-di-api

How to add data in UDT of SAP B1 in SDK?


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


Solution

  • 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.