Subject: LotusScript - IF & Else
You’re missing more than one “End If”. In LotusScript (and in Visual Basic), each If block must end with an End If. In this example, you actually want to use “Elseif” rather than “Else … If”:
Sub Initialize
Set item = doc.GetFirstItem(“SendTo”)
If item =“A” Then
Call doc.PutInFolder( “1” )
Elseif item = “B” Then
Call doc.PutInFolder( “2” )
Elseif item = “C” Then
Call doc.PutInFolder( “3” )
Elseif item = “D” Then
Call doc.PutInFolder( “4” )
End If
End Sub
Elseif allows a separate condition evaluation within the same If block.
Now, there’s still a problem, since a NotesItem is an object and will never be equal to a string. You obviously want to check the value stored in the field (item), not the item object
itself. Rather than using a NotesItem variable and assigning its value with GetFirstItem, use a string variable, and assign its value with GetItemValue. Since that will return an array, you’ll need grab an element of the array to assign to your string:
Sub Initialize
sendToString = doc.GetItemValue(“SendTo”)(0)
If sendToString =“A” Then
Call doc.PutInFolder( “1” )
Elseif sendToString = “B” Then
Call doc.PutInFolder( “2” )
Elseif sendToString = “C” Then
Call doc.PutInFolder( “3” )
Elseif sendToString = “D” Then
Call doc.PutInFolder( “4” )
End If
End Sub
There is another construction, though, that’s more appropriate for this sort of thing. When you want to do different things based on the value of a single field/variable, a Select block is much more efficient:
Sub Initialize
sendToString = doc.GetItemValue(“SendTo”)(0)
Select Case sendToString
Case “A”
Call doc.PutInFolder( “1” )
Case “B”
Call doc.PutInFolder( “2” )
Case “C”
Call doc.PutInFolder( “3” )
Case “D”
Call doc.PutInFolder( “4” )
Case Else
End Select
End Sub
HOWEVER – “doc” is magic? In LotusScript, you don’t get free stuff. Notes doesn’t know what “doc” is or where to find “doc” until you tell it. You also need to declare your variables unless you’re into nasty surprises.
If this code is running from a document open in the user interface, you’d need to do something like this:
(Options)
Option Public
Option Declare
'never, ever leave th Option declare out
'set your Programmer’s Pane preferences to
'always add Option Declare
Sub Initialize
Dim ws As New NotesUIWorkspace
Dim uiDocument As NotesUIDocument
Dim doc As NotesDocument
Dim sendToString As String
Set uiDocument = ws.CurrentDocument
Set doc = uiDocument.Document
sendToString = doc.GetItemValue(“SendTo”)(0)
Select Case sendToString
Case “A”
Call doc.PutInFolder( “1” )
Case “B”
Call doc.PutInFolder( “2” )
Case “C”
Call doc.PutInFolder( “3” )
Case “D”
Call doc.PutInFolder( “4” )
Case Else
End Select
End Sub
In different contexts, you’d get “doc” differently. It’ll come eventually – just keep plugging away.