cancel
Showing results for 
Search instead for 
Did you mean: 

Recordset throwing error

Former Member
0 Kudos

I am trying to build an addon that does a simple check on a new quote/sales order being added or updated to validate the business partner.

In essence I take the cardcode off the form and check the business partner table to see if Accounts want to stop any orders going out.

I am a little rusty as 6 years since my last addon but I have followed some of the SDK examples.

My code below throws the error: "Object reference not set to an instance of an object"

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

        Dim oObj As SAPbobsCOM.SBObob

        Dim rs As SAPbobsCOM.Recordset

        Try

            If (pVal.FormType = 139 And pVal.EventType <> SAPbouiCOM.BoEventTypes.et_FORM_UNLOAD) Then

                If ((pVal.ItemUID = "1" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And (pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE)) And (pVal.BeforeAction = True)) Then

                    '//get the event sending form

                    oSalesForm = oApp.Forms.GetFormByTypeAndCount(pVal.FormType, pVal.FormTypeCount)

                    sCardCode = oSalesForm.Items.Item("4").Specific.String

                    rs = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

                    rs.DoQuery("SELECT U_OnHold FROM OCRD WHERE CardCode = '" & sCardCode & "'")

                    sOnHold = rs.Fields.Item(0).Value

                    If sOnHold = "Y" Then

                        MsgBox("Business Partner is ON HOLD, Please contact accounts to approve this document", MsgBoxStyle.Exclamation, "BP On Hold")

                    End If

                End If

            End If

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Beni,

Yes I have defined those, both are in the same class. If I remove the following lines:

rs = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

                    rs.DoQuery("SELECT U_OnHold FROM OCRD WHERE CardCode = '" & sCardCode & "'")

                    sOnHold = rs.Fields.Item(0).Value

And just have a message box displaying this works fine.

For completeness, here is the whole class:

Friend Class OnHoldManager

    Private WithEvents oApp As SAPbouiCOM.Application

    Private oCompany As SAPbobsCOM.Company

    Private oQuoteForm As SAPbouiCOM.Form

    Private oSalesForm As SAPbouiCOM.Form

    Dim strResult As String

    Dim errResult As String

    Dim sCardCode As String

    Dim sOnHold As String

   

    Private Sub SetApplication()

        '*******************************************************************

        '// Use an SboGuiApi object to establish connection

        '// with the SAP Business One application and return an

        '// initialized appliction object

        '*******************************************************************

        Dim oSAPGui As SAPbouiCOM.SboGuiApi

        Dim sConnectionString As String

        oSAPGui = New SAPbouiCOM.SboGuiApi

        '// by following the steps specified above, the following

        '// statment should be suficient for either development or run mode

        sConnectionString = Environment.GetCommandLineArgs.GetValue(1)

        'sConnectionString = Command()

        '// connect to a running SBO Application

        oSAPGui.Connect(sConnectionString)

        '// get an initialized application object

        oApp = oSAPGui.GetApplication()

    End Sub

    Private Function SetConnectionContext() As Integer

        Dim sCookie As String

        Dim sConnectionContext As String

        'Dim lRetCode As Integer

        oCompany = New SAPbobsCOM.Company

        sCookie = oCompany.GetContextCookie

        sConnectionContext = oApp.Company.GetConnectionContext(sCookie)

        If oCompany.Connected = True Then

            oCompany.Disconnect()

        End If

        SetConnectionContext = oCompany.SetSboLoginContext(sConnectionContext)

    End Function

    Private Function ConnectToCompany() As Integer

        ConnectToCompany = oCompany.Connect '// Establish the connection to the company database.

    End Function

    Public Sub New()

        MyBase.New()

        '//*************************************************************

        '// set SBO_Application with an initialized application object

        '//*************************************************************

        SetApplication()

    End Sub

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

        Dim oObj As SAPbobsCOM.SBObob

        Dim rs As SAPbobsCOM.Recordset

        Try

           

            If (pVal.FormType = 139 And pVal.EventType <> SAPbouiCOM.BoEventTypes.et_FORM_UNLOAD) Then

                If ((pVal.ItemUID = "1" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And (pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE)) And (pVal.BeforeAction = True)) Then

                    '//get the event sending form

                    oSalesForm = oApp.Forms.GetFormByTypeAndCount(pVal.FormType, pVal.FormTypeCount)

                    sCardCode = oSalesForm.Items.Item("4").Specific.String

                    rs = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

                    rs.DoQuery("SELECT U_OnHold FROM OCRD WHERE CardCode = '" & sCardCode & "'")

                    sOnHold = rs.Fields.Item(0).Value

                    If sOnHold = "Y" Then

                        MsgBox("Business Partner is ON HOLD, Please contact accounts to approve this document", MsgBoxStyle.Exclamation, "BP On Hold")

                    End If

                End If

            End If

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

    Private Sub oApp_AppEvent(ByVal EventType As SAPbouiCOM.BoAppEventTypes) Handles oApp.AppEvent

        Try

            Select Case EventType

                Case SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged, SAPbouiCOM.BoAppEventTypes.aet_ServerTerminition, SAPbouiCOM.BoAppEventTypes.aet_ShutDown

                    System.Windows.Forms.Application.Exit()

            End Select

        Catch ex As Exception

            MsgBox(ex.ToString)

        End Try

    End Sub

    Protected Overrides Sub Finalize()

        MyBase.Finalize()

    End Sub

End Class

ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi David,

Make sure you are connected to the company and the RecordSet object is declared correctly.

Thanks & Regards

Ankit Chauhan

Former Member
0 Kudos

Hi David,

I didn't see any call to SetConnectionContext  and ConnectToCompany in the attached code. This means that oCompany has never initialized!

Beni.

Answers (3)

Answers (3)

Former Member
0 Kudos

Thanks Beni, I will have another look through the SDK examples and see where I missed these out.

Dave A.

pvsbprasad
Active Contributor
0 Kudos

Hi,

Object reference not set to an instance of an object-> is declaration problem...

i think declaration problem in sap screen


dim objform as sapboui.form

DIm omatrix as sapboui.matrix


So may i knw the line where ur getting the error.


Regards,

Prasad

Former Member
0 Kudos

Hi David,

When you're running in debug mode - in which line does it fail?

Beni.

Former Member
0 Kudos

Hi,

In debug mode with breaks the error is thrown on line rs = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset) which seems pretty standard.

oCompany is defined and used at the start as standard:

Friend Class OnHoldManager

    Private WithEvents oApp As SAPbouiCOM.Application

    Private oCompany As SAPbobsCOM.Company

    Private oQuoteForm As SAPbouiCOM.Form

    Private oSalesForm As SAPbouiCOM.Form

    Dim strResult As String

    Dim errResult As String

    Dim sCardCode As String

    Dim sOnHold As String

   

    Private Sub SetApplication()

        '*******************************************************************

        '// Use an SboGuiApi object to establish connection

        '// with the SAP Business One application and return an

        '// initialized appliction object

        '*******************************************************************

        Dim oSAPGui As SAPbouiCOM.SboGuiApi

        Dim sConnectionString As String

        oSAPGui = New SAPbouiCOM.SboGuiApi

        '// by following the steps specified above, the following

        '// statment should be suficient for either development or run mode

        sConnectionString = Environment.GetCommandLineArgs.GetValue(1)

        'sConnectionString = Command()

        '// connect to a running SBO Application

        oSAPGui.Connect(sConnectionString)

        '// get an initialized application object

        oApp = oSAPGui.GetApplication()

    End Sub

    Private Function SetConnectionContext() As Integer

        Dim sCookie As String

        Dim sConnectionContext As String

        'Dim lRetCode As Integer

        oCompany = New SAPbobsCOM.Company

        sCookie = oCompany.GetContextCookie

        sConnectionContext = oApp.Company.GetConnectionContext(sCookie)

        If oCompany.Connected = True Then

            oCompany.Disconnect()

        End If

        SetConnectionContext = oCompany.SetSboLoginContext(sConnectionContext)

    End Function

    Private Function ConnectToCompany() As Integer

        ConnectToCompany = oCompany.Connect '// Establish the connection to the company database.

    End Function

Dave A.

Former Member
0 Kudos

Hi,

Sorry to ask such trivial questions, but are u sure you've called SetConnectionContext  and  ConnectToCompany on load?

Also, are the 2nd piece of code is defined in the same class as the 1st one?

Beni.