Lesson 3: Sending an e-mail message

This is the third of three lessons designed to introduce you to using the LotusScript® language in Domino®. You should already have completed Lessons 1 and 2.

Lesson 3 helps you create a script that makes and sends an e-mail message. The script runs when a user chooses an action on a form.

Step A: Create a form action

Users can invoke a form action any time a document is displayed with the form.

  1. Open the "Learning LotusScript®" database if it does not appear in the Design pane.
  2. Select "Forms" in the Design pane, then open the "Main Topic" form in the Work pane. The form appears at the beginning of the Work pane. The Programmer's pane appears.
  3. Choose Create - Action. The actions appear on the screen, and the Action properties box appears.
  4. In the Action properties box, name the action "Request more info." Close or move the properties box.
  5. Select LotusScript® from the Run menu in the Programmer's pane.
  6. Select "Click" from the list of programmable events on the Objects tab if it is not already selected.

Step B: Enter the script

  1. Edit the subroutine so that it looks exactly like this (except for capitalization, which does not matter).
    Sub Click(Source As Button)
        Dim db As NotesDatabase
        Dim doc As NotesDocument
        ' get the database
        Set db = New NotesDatabase( "", "Learning LotusScript.nsf" )
        ' create a new document in the database
        Set doc = New NotesDocument( db )
        ' set the new document's form so it'll be readable as a mail memo
        doc.Form = "Memo"
        ' set the new document's subject
        doc.Subject = "Request for info"
        ' set the new document's body
        doc.Body = "Please send me more information about this."
        ' mail the new document
        Call doc.Send( False, "Reuben Jordan" )
    End Sub
  2. In the second-to-last line of the script, change the second parameter of the Send method from "Reuben Jordan" to your name.

Step C: Compile and test the script

Do the following:

  1. Choose File - Save.
  2. If you get the message "Data not saved due to script error(s)," check the error at the end of the Programmer's pane, then double-check your typing to make sure that the script looks exactly like the one in Step B.
  3. Choose Design - Preview in Notes®. Alternatively, you can open the database in the Notes® client and open or create a document based on the "Main Topic" form.
  4. Click the "Request more info" action on the action bar. The script sends mail to you.
  5. Check your mail database to make sure the memo is sent. This may take awhile depending on your mail system.

Step D: Edit the script

You may not need to edit your script after saving it, but if you do, here's how.

  1. Open the "Learning LotusScript®" database if it does not appear in the Design pane.
  2. Select "Forms" in the Design pane, then open the "Main Topic" form in the Work pane.
  3. If the actions do not appear, drag the outline edge of the pane to the left until you can see the names of the actions.
  4. Select the "Request more info" action.

Review: How did the script work?

The script you just created means: "I want to create a new document in the database, set the value of some of its items, and mail it."

The script is commented in Step B. In addition, notice the following:

  • The script responds to a click event on a form action. It will also respond to a user who chooses the action from the Notes® client menu.
  • Using New with the NotesDocument class actually creates a new document. This behavior is different from that of NotesDatabase, where New does not actually create a new database.
  • The script creates and sets the value of several items on the doc object by treating their names as properties of doc. For example, doc.Form = "Memo" creates an item called Form on the new document and sets its value to "Memo." The item is of type text, since "Memo" is a string. If a Form item already existed on the document, this statement changes its value to "Memo."
  • Send is a method in the NotesDocument class that mails a document. It does not have a return value so you call it as a subroutine.

Challenge: Sending an e-mail message to the author of the current document

The script you just wrote sends mail to you, regardless of what document is currently open in the database. Modify the script so that it sends mail to the author of the current document.

To do this, you need a way to access the current document (the one that's open on the workspace when the user clicks the "Request more info" button on the action bar). So far, you've used the NotesDocument class to represent documents stored in a database. This class can be used to access any document that's stored in the database. Domino® uses a different class (with different properties and methods) to represent the document that's currently open on the workspace. Using the Reference tab, find the classes, methods, and properties required to:

  • Access the current document
  • Access the From field on the current document (this is where the author's name is stored)

If you need a hint, look under NotesUIWorkspace class.

Solution: Sending an e-mail message to the author of the current document

One correct way to write the script is as follows:

Sub Click(Source As Button)
    ' NEW: access the document that's currently open on the workspace
    Dim workspace As New NotesUIWorkspace
    Dim uidoc As NotesUIDocument
    Set uidoc = workspace.CurrentDocument
    ' UNCHANGED from previous script
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    ' get the database
    Set db = New NotesDatabase( "", "Learning LotusScript.nsf" )
    ' create a new document in the database
    Set doc = New NotesDocument( db )
    ' set the new document's form so it'll be readable as a mail memo
    doc.Form = "Memo"
    ' set the new document's subject
    doc.Subject = "Request for info"
    ' set the new document's body
    doc.Body = "Please send me more information about this."
    ' mail the new document
    ' NEW: use the NotesUIDocument object
    ' to get the value of the From field
    Call doc.Send (False, uidoc.FieldGetText( "From" ))
End Sub