Sample Code for Action Button from Approval Application using LotusScript for Validation

Author: Tripp W Black

Created: 02/08/2003 at 01:58 PM

 

Category:
Notes Developer Tips
Actions, LotusScript

Sample Code for Action Button from Approval Application using LotusScript for Validation

This code calls a validation routine stored in the subform or form in which the button resides. Once that call is successful the processing of the form to the next approver level is performed.


Button Code:
Sub Click(Source As Button)
Dim s As New NotesSession
Dim w As New NotesUIWorkspace
Dim uiDoc As NotesUIDocument ' current frontend document being acted upon
Dim db As NotesDatabase ' this database
Dim doc As NotesDocument ' backed doc to this frontend document
Dim success As Integer ' test for validation pass, 1 for pass, 0 for fail
Dim tmpstring As String ' temporary holding string
Dim tmplist() As String ' temporary list for assign fields
Dim tmpcount As Integer ' temporary counting variable
Dim authorsItem As NotesItem ' needed to assign authors list
Dim historyVariant As Variant ' needed to get old history
Dim historyItem As NotesItem ' needed to assing workflow history list

Stop ' needed for debub mode to pop up the debugger
' setup environment
Set db = s.CurrentDatabase
Set uiDoc = w.CurrentDocument
Set doc=uiDoc.Document ' we are moving to backend, from this point onward - no frontend messing with uidoc

' validate subform
success = ValidateFields(uidoc)

' test success to continue or exit
If success=1 Then
' continue
Print ("Passed Validation")
Else
' quit
Exit Sub
End If

' test for edit mode
If uidoc.EditMode Then
' we can continue
Else
' quit
Exit Sub
End If

' update fields - we have to go backend to update these
uidoc.AutoReload=False
doc.agentExecute = "1"
doc.mailExecute = "1"
doc.status = "Liason"
tmpstring= uidoc.FieldGetText("deptLiason")
doc.nextReviewer = tmpstring
doc.requestActionDate = Today()
' process authors
Redim tmpList(2)
tmpList(0) = tmpstring
tmpList(1) = "[admin]"
tmpList(2) = "LocalDomainServers"
Set authorsItem=doc.ReplaceItemValue("authors", tmpList)
' process workflow
Redim tmpList(0)
tmpcount=0
historyVariant = doc.GetItemValue("workflowHistory")
Forall h In historyVariant
Redim Preserve tmpList(tmpcount)
tmpList(tmpcount) = h
tmpcount=tmpcount+1
End Forall
' add the newest history of this button
If tmpList(0) = "" Then
' there was no previous history so we would have a "" hole if we didn't start again from 0
Redim Preserve tmpList(0)
tmpcount=0
Else
Redim Preserve tmpList(tmpcount)
End If
tmpList(tmpcount) = Now & " -" & s.CommonUserName & " (HR) submitted to liason for approval."
Set historyItem=doc.ReplaceItemValue("workflowHistory", tmpList)
Call uidoc.Reload
uidoc.AutoReload=True
Call uidoc.Save
Call uidoc.Close
End Sub



Validation Code (put in global code are of subform/form)

Function ValidateFields(uidoc As NotesUIDocument) As Integer
' set fields to check for null
Dim valFields() As String ' string list of fields to check for null/empty
Dim valMsg() As String ' string list of messages for the valFields() string list
Dim valcheck As String ' value of a field being tested for validation
Dim valchangetype As String ' value of changeType to compare for validation
Dim xcount As Integer ' termporary counting variable for loops
Redim valFields(3)
Redim valMsg(3)
valFields(0) = "changeType" ' test for null
valFields(1) = "Name" ' test for null
valFields(2) = "dept_1" ' test for null
valFields(3) = "effectiveDate" ' test for null
valMsg(0) = "You must select the type of change"
valMsg(1) = "The staff/employee's name field is required."
valMsg(2) = "Selection for a new/existing department is required."
valMsg(3) = "This change request must have an effective date to begin."

' check these fields
xcount=0
Forall xval In valFields
' loop through and check each one for null
valcheck = uidoc.FieldGetText(xval)
If valcheck="" Then
' notify user
Messagebox(valMsg(xcount))
Call uidoc.GotoField( xval )
ValidateFields=0
Exit Function
End If
xcount=xcount+1
End Forall

' the rest of fields depend on the value of changeType
valchangetype=uidoc.FieldGetText("changeType")
If valchangetype="" Then
' it should have been caught above but if not we will error and exit now.
Messagebox("The Change Type is not selected. It must be selected before processing")
Call uidoc.GotoField( "changeType" )
ValidateFields=0
Exit Function
End If

Select Case valchangetype
Case "Name Change"
' check to see if the following fields are null
Redim valFields(0)
Redim valMsg(0)
valFields(0) = "newName" ' test for null if changeType="Name Change"
valMsg(0) = "The staff's new name is required."
xcount=0
Forall xval In valFields
' loop through and check each one for null
valcheck = uidoc.FieldGetText(xval)
If valcheck="" Then
' notify user
Messagebox(valMsg(xcount))
Call uidoc.GotoField( xval )
ValidateFields=0
Exit Function
End If
xcount=xcount+1
End Forall
Case "Moving to New Position"
' check to see if the following fields are null
Redim valFields(5)
Redim valMsg(5)
valFields(0) = "oldTitle" ' test for null if changeType="Moving to New Position"
valFields(1) = "newTitle" ' test for null if changeType="Moving to New Position"
valFields(2) = "deptOld" ' test for null if changeType="Moving to New Position"
valFields(3) = "buildingLocation" ' test for null if changeType="Moving to New Position"
valFields(4) = "newOfficeLocation" ' test for null if changeType="Moving to New Position"
valFields(5) = "phoneExt" ' test for null if changeType="Moving to New Position"
valMsg(0) = "The old position title is required."
valMsg(1) = "The new position title is required."
valMsg(2) = "The staff's old department name is required."
valMsg(3) = "The staff's old building location is required."
valMsg(4) = "The staff's new building location is required."
valMsg(5) = "The staff's phone extension is required."
xcount=0
Forall xval In valFields
' loop through and check each one for null
valcheck = uidoc.FieldGetText(xval)
If valcheck="" Then
' notify user
Messagebox(valMsg(xcount))
Call uidoc.GotoField( xval )
ValidateFields=0
Exit Function
End If
xcount=xcount+1
End Forall
Case "On Leave"
' check to see if the following fields are null
Redim valFields(0)
Redim valMsg(0)
valFields(0) = "returnDate" ' test for null if changeType="On Leave" | changeType="Leave Without Pay"
valMsg(0) = "The staff's return date is required."
xcount=0
Forall xval In valFields
' loop through and check each one for null
valcheck = uidoc.FieldGetText(xval)
If valcheck="" Then
' notify user
Messagebox(valMsg(xcount))
Call uidoc.GotoField( xval )
ValidateFields=0
Exit Function
End If
xcount=xcount+1
End Forall
Case "Leave Without Pay"
' check to see if the following fields are null
Redim valFields(0)
Redim valMsg(0)
valFields(0) = "returnDate" ' test for null if changeType="On Leave" | changeType="Leave Without Pay"
valMsg(0) = "The staff's return date is required."
xcount=0
Forall xval In valFields
' loop through and check each one for null
valcheck = uidoc.FieldGetText(xval)
If valcheck="" Then
' notify user
Messagebox(valMsg(xcount))
Call uidoc.GotoField( xval )
ValidateFields=0
Exit Function
End If
xcount=xcount+1
End Forall
Case Else
' let it pass do nothing
End Select
ValidateFields=1 ' success
End Function

previous page