Option Public
Uselsx "*lsxlc"
'Global SAP connection
Dim sapsession As lcconnection
'Global LCSession object - used for error reporting
Dim session As lcsession
Sub Initialize
Dim NotesSession As New NotesSession
Dim dbCurr As NotesDatabase
Dim viewCust As NotesView
Dim docCurr As NotesDocument
'Formally create our global LCSession object
Set session = New LCSession()
Set dbCurr = NotesSession.CurrentDatabase
'Get this view, which only contains our user edited documents
'which need to be sent to SAP
Set viewCust = dbCurr.GetView("(HASBEENCHANGED)")
'Get one of them
Set docCurr = viewCust.GetFirstDocument()
'AssertConnection gets us a connection the first time and simply
'returns when called again
While Not (docCurr Is Nothing) And AssertConnection
'This is the core of it all - do all that is needed to change this
'Employee address in SAP
If ChangeAddress(docCurr) Then
'Upon success, remove this field so we don't try to send the
'change again the next time that this agent runs
Call docCurr.Removeitem("HASBEENCHANGED")
Call docCurr.Save(True, True)
Print Str$(doc.PERNR) + " address data successfully changed"
Else
Print "Error changing "+ Str$(doc.PERNR) + " address data"
End If
'Because we are one at a time removing docs from the view
'we can't use the GetNextDocument method
Set docCurr = viewCust.GetFirstDocument()
Wend
Print "Agent complete"
End Sub
Function AssertConnection As Integer
'This function tests if there is an active server object connected to an R/3
'server. If not, a new global server object is created and connected to an R/3 server,
'using the settings of the active logon setup, choosen in the view
'Administration/R/3 Logon Setup.
On Error Goto errorhandler
'Check if there is already an active server object
If Not (sapsession Is Nothing) Then
'and if so is it connected to an R/3 system?
If sapsession.IsConnected = True Then
AssertConnection = True
Exit Function
End If
End If
'Create the server object and fill up the necessary properties for logon.
Set sapsession = New LCConnection("sap")
sapsession.Userid = "smorris"
sapsession.Password = "XXXX"
sapsession.Client="800"
sapsession.Destination="LD3"
sapsession.SystemNo=0
sapsession.Language="EN"
sapsession.Server="sapaix.lotus.com"
'logon
sapsession.Connect
AssertConnection = True
Exit Function
errorhandler:
Dim Msg As String
Dim Msgcode As Long
Dim status As Integer
Dim result As String
If session.status <> LCSUCCESS Then
status = session.GetStatus(result, Msgcode, Msg)
Print result
End If
AssertConnection = False
End Function
Function ChangeAddress (doc As NotesDocument) As Integer
'We will write to SAP in this field list
Dim INPUTFIELDLIST As New LCFieldList
'SAP talks to us via this field list
Dim OUTPUTFIELDLIST As New LCFieldList
On Error Goto errorhandler
ChangeAddress = 0
'We must first lock the employee record
sapsession.Database = "BAPI_EMPLOYEET_ENQUEUE"
sapsession.Metadata = "*"
sapsession.MapByName = True
Dim UserPERNR As LCField
'IMPORTSNUMBER is how we reference import parameter NUMBER
Set UserPERNR = INPUTFIELDLIST.Append ("IMPORTSNUMBER",LCTYPE_NUMERIC)
'Take the value from the doc
UserPERNR.Value = doc.PERNR
Dim ValidityBegin As New lcdatetime
Call ValidityBegin.SetCurrent()
Dim VBfield As LCField
Set VBfield = INPUTFIELDLIST.Append ("IMPORTSVALIDITYBEGIN",LCTYPE_DATETIME)
'Lock the record as of today
Call VBfield.setdatetime(1,ValidityBegin )
Dim count As Integer
'Have SAP execute BAPI_EMPLOYEET_ENQUEUE
count = sapsession.call(INPUTFIELDLIST, 1, OUTPUTFIELDLIST)
Dim retfield As lcfield
'This field will tell us if we have success or failure
Set retfield = OUTPUTFIELDLIST.lookup("EXPORTSRETURNNUMBER")
Call sapsession.Fetch (OUTPUTFIELDLIST)
'0 is success. Export structure RETURN could return error text in the case of
'error.In this example we will just hard code our error text in this case.
If retfield.Value(0) <> 0 Then
Print "Unable to lock Employee"
doc.ERRORTEXT = "Unable to lock Employee"
Call doc.Save(True,True)
Exit Function
End If
'Get ready for the next BAPI call on this connection
Call sapsession.Action(LCACTION_RESET)
'BAPI_ADDRESSEMP_CHANGE is what we need now
sapsession.Database = "BAPI_ADDRESSEMP_CHANGE"
sapsession.Metadata = "*"
sapsession.MapByName = True
Delete INPUTFIELDLIST
Set INPUTFIELDLIST = New LCFieldList
Delete OUTPUTFIELDLIST
Set OUTPUTFIELDLIST = New LCFieldList
Dim SUBTYPE As LCField
Set UserPERNR = INPUTFIELDLIST.Append ("IMPORTSEMPLOYEENUMBER",LCTYPE_NUMERIC)
UserPERNR.Value = doc.PERNR
'Get SUBTYPE from the doc
Set SUBTYPE = INPUTFIELDLIST.Append ("IMPORTSSUBTYPE",LCTYPE_TEXT)
SUBTYPE.Text = doc.SUBTY(0)
'Get date from doc
Set ValidityBegin = New LCDatetime(Year(doc.BEGDA(0)), Month(doc.BEGDA(0)),
Day(doc.BEGDA(0)))
Set VBfield = INPUTFIELDLIST.Append ("IMPORTSVALIDITYBEGIN",LCTYPE_DATETIME)
Call VBfield.setdatetime(1,ValidityBegin )
Dim VEField As LCField
Dim ValidityEnd As New LCDatetime(9999,12,31)
Set VEfield = INPUTFIELDLIST.Append ("IMPORTSVALIDITYEND",LCTYPE_DATETIME)
'Set date to SAP max date
Call VEfield.setdatetime(1,ValidityEnd )
Dim STREET As LCField
Set STREET = INPUTFIELDLIST.Append("IMPORTSSTREETANDHOUSENO",LCTYPE_TEXT)
'Get the street from the doc
STREET.Text = doc.STREET(0)
Dim CITY As LCField
Set CITY = INPUTFIELDLIST.Append("IMPORTSCITY",LCTYPE_TEXT)
'Get the city from the doc
CITY.Text = doc.CITY(0)
Dim STATE As LCField
Set STATE = INPUTFIELDLIST.Append("IMPORTSSTATE",LCTYPE_TEXT)
'Get the state from the doc
STATE.Text = doc.STATE(0)
Dim POSTALCODECITY As LCField
Set POSTALCODECITY = INPUTFIELDLIST.Append("IMPORTSPOSTALCODECITY",LCTYPE_TEXT)
'Get the zipcode from the doc
POSTALCODECITY.Text = doc.ZIPCODE(0)
'Have SAP execute BAPI_ADDRESSEMP_CHANGE
count = sapsession.call(INPUTFIELDLIST, 1, OUTPUTFIELDLIST)
'This field will tell us if we have success or failure
Set retfield = OUTPUTFIELDLIST.lookup("EXPORTSRETURNNUMBER")
Call sapsession.Fetch (OUTPUTFIELDLIST)
'0 is success. Export structure RETURN could return error text in the case of
'error. In this example we will just hard code our error text in this case.
If retfield.Value(0) <> 0 Then
Print "Error changing address"
doc.ERRORTEXT = "Error changing address"
Call doc.Save(True,True)
Exit Function
End If
'Things are looking good. Get the connection ready for the last step
Call sapsession.Action(LCACTION_RESET)
'This will unlock the employee record
sapsession.Database = "BAPI_EMPLOYEET_DEQUEUE"
sapsession.Metadata = "*"
sapsession.MapByName = True
Delete INPUTFIELDLIST
Set INPUTFIELDLIST = New LCFieldList
Delete OUTPUTFIELDLIST
Set OUTPUTFIELDLIST = New LCFieldList
Set UserPERNR = INPUTFIELDLIST.Append ("IMPORTSNUMBER",LCTYPE_NUMERIC)
UserPERNR.Value = doc.PERNR
Call ValidityBegin.SetCurrent()
Set VBfield = INPUTFIELDLIST.Append ("IMPORTSVALIDITYBEGIN",LCTYPE_DATETIME)
Call VBfield.setdatetime(1,ValidityBegin )
count = sapsession.call(INPUTFIELDLIST, 1, OUTPUTFIELDLIST)
'This field will tell us if we have success or failure
Set retfield = OUTPUTFIELDLIST.lookup("EXPORTSRETURNNUMBER")
Call sapsession.Fetch (OUTPUTFIELDLIST)
'0 is success. Export structure RETURN could return error text in the case of
'error.In this example we will just hard code our error text in this case.
If retfield.Value(0) <> 0 Then
Print "Unable to unlock Employee"
Exit Function
End If
ChangeAddress = 1
Exit Function
errorhandler:
Dim Msg As String
Dim Msgcode As Long
Dim status As Integer
Dim result As String
If session.status <> LCSUCCESS Then
status = session.GetStatus(result, Msgcode, Msg)
Print result
Else
Print "Error exit with no status"
End If
End Function
|