📚 SAP Business One SDK Help

Adding a new activity sample
See Also

Description

The following sample code shows how to add an activity with a business partner.
Private Sub cmdTest_Click()
   On Error GoTo ErrorHandler
   Dim vCompany As SAPbobsCOM.Company

   'create company object
   Set vCompany = New SAPbobsCOM.Company

   'set paras for connection
   vCompany.CompanyDB = "SBODemo_US"
   vCompany.Password = "manager"
   vCompany.UserName = "manager"
   vCompany.Server = "(local)"

   'connect to database server
   If (0 <> vCompany.Connect()) Then
      MsgBox "Failed to connect"
      Exit Sub
   End If

   Dim nErr As Long
   Dim errMsg As String

   'We will set up a contact with BP D10008, on Dec 15, 2002
   'if the contact already exists, then just refresh it, else
   'add it.
   Dim bFound As Boolean
   bFound = False
   Dim iNum As Long
   iNum = 1
   Dim vContact As SAPbobsCOM.Contacts
   Set vContact = vCompany.GetBusinessObject(oContacts)
   While ((vContact.GetByKey(iNum) = True) And (bFound = False))
       If (vContact.CardCode = "HU1001") Then
           If (vContact.Closed = tNO) Then
               bFound = True
           End If
       End If
       iNum = iNum + 1
   Wend
   vContact.CardCode = "HU1001"
   vContact.Closed = tNO
   vContact.ContactDate = CDate("15/12/2002")
   vContact.Notes = "Discuss next year's financial plan"
   If (bFound = False) Then
       'can't find an existing contact to use, just creat a new one
       vContact.DocType = oContacts
       If (vContact.Add() <> 0) Then
           MsgBox "Failed to add a contact"
       Else
           MsgBox ("Succeeded in add contact")
           vContact.SaveXml ("C:\temp\Contact" + vContact.DocEntry + ".xml")
       End If
   Else
       If (0 <> vContact.Update()) Then
           MsgBox ("Failed on contact update")
       Else
           MsgBox ("Succeeded in update contact")
           vContact.SaveXml ("C:\temp\Contact" + vContact.DocEntry + ".xml")
       End If
   End If
   'Check Error
   Call vCompany.GetLastError(nErr, errMsg)
   If (0 <> nErr) Then
       MsgBox ("Found error:" + Str(nErr) + "," + errMsg)
   End If

   'disconnect the company object, and release resource
   Call vCompany.Disconnect
   Set vCompany = Nothing
   Exit Sub
ErrorHandler:
   MsgBox ("Exception:" + Err.Description)
End Sub