on 07-04-2014 1:41 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Beni, I will have another look through the SDK examples and see where I missed these out.
Dave A.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi David,
When you're running in debug mode - in which line does it fail?
Beni.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
User | Count |
---|---|
100 | |
11 | |
11 | |
6 | |
6 | |
5 | |
4 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.