This simple LotusScript agent demonstrates how you can refresh all the docs in a given view. For the sake of this example, the AllDocs view is used. However, you can use this script to work with any view. You might need to run this on a view that contains a subset of documents or documents that meet a certain criteria as declared in a selection formula in a view. For example, you might have an approval workflow routine that specifies that if a document isn't acted upon within a given window of time, something should be done to the document, whether routing it somewhere or sending a late notification to the approver. This script can help by refreshing each document in the database. Let's say that each night at 12:15 a.m. EST you want the RefreshAllDocs agent to run. When it runs, the agent updates all the computed fields in each document that resides in the view and saves them. Refreshing critical fields in this way would enable you to programmatically trigger another agent or action to occur when these values change.
The LotusScript options shown in Listing 15.18 are used as previously shown in Listing 15.1.
Listing 15.18 Option Preferences
1. Option Public 2. Option Explicit
The global declarations for this script are as follows :
Each declared variable as shown in Listing 15.19 will be used throughout the script.
Listing 15.19 Global Declarations
1. Dim db As NotesDatabase 2. Dim view As NotesView 3. Dim currDoc As NotesDocument 4. Dim nextDoc As NotesDocument 5. Dim i As Integer 6. Dim errormsg As String
In line 8 of Listing 15.20, an interative While Not method is used to loop through each document in the view. Line 7 shows the i integer variable being set to 1 . As the agent progresses through the While Not loop, the integer is incremented by 1 at each pass, as shown in line 8.vi. This is to be used later as a count to show how many documents were processed in the view by the agent. Line 8.i, currDoc.ComputeWithForm false,false , is the command that refreshes the fields on the document. After it has refreshed the agent, it gets the next document in the view, resets the currDoc variable to the next document object found, and clears the nextDoc variable so that it can be used again. Lines 8 “9 are typical ways to use the While Not and While methods . In LotusScript, when you have a handle on an object, you must set the new object to a temporary object, discard the old object, set the old variable to the new temporary object, and then discard the temporary object, as shown in line 8.v.
Listing 15.20 The Initialize Subroutine
1. Sub Initialize 2. Dim session As New NotesSession 3. On Error Goto ERRORHANDLER 4. Set db = session.CurrentDatabase 5. Set view = db.GetView("AllDocs") 6. Set currDoc = view.GetFirstDocument 7. i=1 8. While Not(currDoc Is Nothing) i. currDoc.ComputeWithForm False,False ii. currDoc.Save True,False iii. Set nextDoc = view.GetNextDocument(currDoc) iv. Set currDoc = nextDoc v. Set nextDoc = Nothing vi. i=i+1 9. Wend 10. Messagebox ("Agent Logging: " + Cstr(i) + " documents have been updated in MyApp on " + Cstr(Now) ) 11. Exit Sub 12. ERRORHANDLER: 13. errormsg = " * * Agent Error: " & Err & " - " & Error() & _ 14. " in " & "Robot-DailyRefreshAllDocs at " & Erl() 15. Msgbox errormsg 16. Resume Next 17. End Sub
After the While Not loop has completed, a message is logged to the log.nsf file on the server where the agent is being run, telling the administrator how many documents were updated in the application and when. If an error occurs during the agent run, the ERRORHANDLER event is called to log the error to the log.nsf database on the server, as shown in lines 12 “17.
The complete code listing for the Robot-DailyRefreshAllDocs agent is shown in Listing 15.21.
Listing 15.21 The Robot-DailyRefreshAllDocs Agent Complete Script Listing
'Robot-DailyRefreshAllDocs: Option Public Option ExplicitDim db As NotesDatabase Dim view As NotesView Dim currDoc As NotesDocument Dim nextDoc As NotesDocument Dim i As Integer Dim errormsg As String Sub Initialize Dim session As New NotesSession On Error Goto LOGERROR Set db = session.CurrentDatabase Set view = db.GetView("AllDocs") Set currDoc = view.GetFirstDocument i=1 While Not(currDoc Is Nothing) currDoc.ComputeWithForm False,False currDoc.Save True,False Set nextDoc = view.GetNextDocument(currDoc) Set currDoc = nextDoc Set nextDoc = Nothing i=i+1 Wend Messagebox ("Agent Logging: " + Cstr(i) + " documents have been updated in MyApp on " + Cstr(Now) ) Exit Sub LOGERROR: errormsg = " * * Agent Error: " & Err & " - " & Error() & _ " in " & "Robot-DailyRefreshAllDocs at " & Erl() Msgbox errormsg Resume Next End Sub
Part I. Introduction to Release 6
Whats New in Release 6?
The Release 6 Object Store
The Integrated Development Environment
Part II. Foundations of Application Design
Advanced Form Design
Using Shared Resources in Domino Applications
Using the Page Designer
Adding Framesets to Domino Applications
Automating Your Application with Agents
Part III. Programming Domino Applications
Using the Formula Language
Real-World Examples Using the Formula Language
Writing LotusScript for Domino Applications
Real-World LotusScript Examples
Writing Java for Domino Applications
Real-World Java Examples
Enhancing Domino Applications for the Web
Part IV. Advanced Design Topics
Accessing Data with XML
Accessing Data with DECS and DCRs
Security and Domino Applications
Creating Workflow Applications
Analyzing Domino Applications
Part V. Appendices
Appendix A. HTML Reference
Appendix B. Domino URL Reference