LotusScript - IF & Else

Can anyone check this out. I am new to LS and I am trying to figure this out.

Sub Initialize

Set item = doc.GetFirstItem("SendTo")

If item ="A" Then

	Call doc.PutInFolder( "1" )	

Else

	If item = "B" Then

		Call doc.PutInFolder( "2" )	

	Else

		If item = "C" Then

			Call doc.PutInFolder( "3" )

		Else

			If item = "D" Then

				Call doc.PutInFolder( "4" )	

			End If									

End Sub

Getting Error:

Unexpected END SUB; Expected: END IF

I tried to remove END SUB but it just comes back,

Subject: LotusScript - IF & Else

Where and how are you using this script?

It seems a large chunk of your code is missing. Provide some more details and we will be able to give you some more help.

As for the If… Else stuff… look into using Cases instead.

Subject: LotusScript - IF & Else

Scott,

You’d need an END IF for each IF, (drop the ELSE’s, since they serve no purpose there) but it would be better to use SELECT-CASE.

SELECT CASE item

CASE “A”

Call doc.PutInFolder( “1” )

CASE “B”

Call doc.PutInFolder( “2” )

CASE “C”

Call doc.PutInFolder( “3” )

CASE “D”

Call doc.PutInFolder( “4” )

End Select

Hope that clears it up.

Dave

Chub Toad, LLC

Subject: RE: LotusScript - IF & Else

Dave,

You are right…

He could use Else If instead though but the case method is the most appropriate.

Subject: RE: LotusScript - IF & Else

Sub Initialize Dim doc As NotesDocument

Dim item As NotesItem

Set item = doc.GetFirstItem("SendTo")

Select Case item

Case "A"

	Call doc.PutInFolder( "1" )

Case "B"

	Call doc.PutInFolder( "2" )

Case "C"

	Call doc.PutInFolder( "3" )

Case "D"

	Call doc.PutInFolder( "4" )

End Select

End Sub

still error

Subject: What’s the new error?

It can’t be the same error, as there is no IF statement to require an END IF…

Subject: Oh, I see now

Sub InitializeDim doc As NotesDocument

Dim sendto As variant

Set sendto = doc.GetItemValue(“SendTo”)

Select Case sendto (0)

Case “A”

Call doc.PutInFolder( “1” )

Case “B”

Call doc.PutInFolder( “2” )

Case “C”

Call doc.PutInFolder( “3” )

Case “D”

Call doc.PutInFolder( “4” )

End Select

End Sub

When you created item as a NotesItem object and set it to SendTo, you just had a handle to the “object”, not the value. If you set a variant to the item value, you can access it. Keep in mind that SendTo (0) is only the first value in that field if it is a text list…

I wasn’t seeing the forest for the trees.

Dave

Chub Toad, LLC

Subject: RE: Oh, I see now

Those of us what DID spot the forest spent so much time preparing a trail that the trees were gone before we posted…

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.

Subject: My, oh my!

Stan, you’re so much more thorough than I am. Extremely well-written note!

Subject: New To LS?

What’s interesting is that “I am new to LS” keeps coming up:

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/bf869972780b67f985256c5b0061a672?OpenDocument&Highlight=0,maynard

Subject: Well 3 years isn’t that long…

is it?

Subject: *Some people are “New To LS” for a long time (because they don’t get much practice, for example)

Subject: RE: New To LS?

What hell does “I’m new to LotusScript” bothers you? Yes - for some reason LS is like me hitting a brick WALL!!! I go to this Blog for help no feedback on why I write “I’m new to LotusScript” If you don’t want to help don’t!It’s not like I not trying.

Subject: RE: New To LS?

This is what I’m am trying to do!

I am trying to setup an agent that will based upon what email address it is sent to, it will then move the email from the inbox to a certain folder. This agent would be run “When new mail arrives”

Code:

Sub Initialize

Dim doc As NotesDocument

Dim XX As Variant

Set XX = doc.GetItemValue("SendTo")

Select Case XX 

Case "CCRGroup1@xxxxxx.com"

	Call doc.PutInFolder( "CCRgroup1" )

Case "CCRGroup2@xxxxxx.com"

	Call doc.PutInFolder( "CCRgroup2" )

Case "CCRGroup3@xxxxxx.com"

	Call doc.PutInFolder( "CCRgroup3" )

Case "CCRGroup4@xxxxxx.com"

	Call doc.PutInFolder( "CCRgroup4" )

End Select

End Sub

I have a document in the Inbox but when I go and manually run this agent. It comes up with an error " Object Variable not set"

Is it because I’m trying to run it manually?

Subject: RE: New To LS?

Please take another look at my earlier (long) posting. You need to tell Notes how to find “doc”; it’s not automatic, and the method will be different depending on how/when the code runs. In addition, SendTo has to be treated as an array (a multiple value), even when there’s only one value there.

Subject: *It’s OK, don’t take it too seriously. I am new to Java five years after my first Java applet, and I may always be new to it.