Examples: Web agents

  1. This agent is named Change Status to Closed and sets the Status field to the value Closed in all documents that meet the selection criteria. The target is All documents in database.
    FIELD Status := "Closed";
    SELECT @All

    To run the agent, you can enter the OpenAgent URL command wherever URLs are permitted, for example:

    http://localhost/Web+test.nsf/Change+Status+to+Closed?OpenAgent

    Or activate an action or hotspot that contains the following code:

    @Command([ToolsRunMacro]; "(Change Status to Closed)")
  2. A Computed for display field named HeadText initially has the following value:
    "This document is being opened from Notes at " + @Text(@Now)

    The WebQueryOpen event on the form calls the agent "ChangeHeadText." The target is "None." The code is:

    @Command([ToolsRunMacro]; "ChangeHeadText")

    ChangeHeadText contains the following formula. When a document based on this form is opened from a browser, WebQueryOpen causes HeadText to be changed; when a document is opened from Notes, HeadText remains as is.

    FIELD HeadText := 
    "This document is being opened from a browser at " + @Text(@Now);
    @All
  3. This is a LotusScript version of Change Status to Closed. It runs on All documents in database and uses UnprocessedDocuments to get the documents to be processed. The Print statement replaces the Agent done page with its text.
    Sub Initialize
        Dim s As New NotesSession
        Dim db As NotesDatabase
        Dim dc As NotesDocumentCollection
        Dim doc As NotesDocument
        Set db = s.CurrentDatabase
        Set dc = db.UnprocessedDocuments
        Set doc = dc.GetFirstDocument
        Do While Not(doc Is Nothing)
            doc.Status = "Closed"
            Call doc.Save(False, True)
            Set doc = dc.GetNextDocument(doc)
        Loop
        Print "<B>All Status fields set to 'Closed'</B>"
    End Sub
  4. This is a LotusScript version of Change Status to Closed that opens the Main View page in a browser when the agent terminates.
    Sub Initialize
        Dim s As New NotesSession
        Dim db As NotesDatabase
        Dim dc As NotesDocumentCollection
        Dim doc As NotesDocument
        Set db = s.CurrentDatabase
        Set dc = db.UnprocessedDocuments
        Set doc = dc.GetFirstDocument
        Do While Not(doc Is Nothing)
            doc.Status = "Closed"
            Call doc.Save(False, True)
            Set doc = dc.GetNextDocument(doc)
        Loop
        dbname$ = Evaluate("@WebDbName")
        Print "[/" + dbname$ + "/Main+View?OpenView]"
    End Sub
  5. This is a Java version of Change Status to Closed.
    import lotus.domino.*;
    import java.io.PrintWriter;
    import java.util.Vector;
    
    public class JavaAgent extends AgentBase {
    
        public void NotesMain() {
    
            try {
                Session session = getSession();
                AgentContext agentContext = session.getAgentContext();
    
                // (Your code goes here) 
                DocumentCollection dc = 
                    agentContext.getUnprocessedDocuments();
                Document doc = dc.getFirstDocument();
                while (doc != null) {
                    doc.replaceItemValue("Status", "Closed");
                    doc.save(false, true);
                    doc = dc.getNextDocument(doc);
                }
                PrintWriter pw = getAgentOutput();
                Vector v = session.evaluate("@WebDbName");
                pw.println("[/" + v.firstElement() + "/Main+View?OpenView]");
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
  6. This agent parses Query_String to extract one argument, which must be Open or Closed. It must be run with an OpenAgent URL command, for example,
    http://localhost/Web+test.nsf/Change+Status?OpenAgent&Closed

    Here is the code:

    Sub Initialize
        Dim s As New NotesSession
        Dim db As NotesDatabase
        Dim dc As NotesDocumentCollection
        Dim doc As NotesDocument
        Dim arg As String, p1 As Long
        arg = s.DocumentContext.Query_String(0)
        p1 = Instr(arg, "&")
        If p1 = 0 Then
            Print "Need argument 'Open' or 'Closed'"
            Exit Sub
        Else
            arg = Lcase(Mid$(arg, p1 + 1))
            If arg <> "open" And arg <> "closed" Then
                Print "Argument must be 'Open' or 'Closed'"
                Exit Sub
            End If
        End If
        arg = Ucase(Left$(arg, 1)) + Right$(arg, Len(arg) - 1)
        Set db = s.CurrentDatabase
        Set dc = db.UnprocessedDocuments
        Set doc = dc.GetFirstDocument
        Do While Not(doc Is Nothing)
            doc.Status = arg
            Call doc.Save(False, True)
            Set doc = dc.GetNextDocument(doc)
        Loop
        Print "<B>Status changed to "+ arg + " in all documents</B>"
    End Sub