Running agent in view or open document
There have been a few times when we wanted to run the exact same agent in a view or against the open document. For example, an "in progress" document needs to be marked as "closed". You may want to run this kind of agent against one or more documents in a view, but you may also want to run this agent against the currently opened document. To do this, create a new agent. Set the agent to be manually run from the agent list (so it's hidden), and set it up to be an agent run against selected documents. Then let your code determine if it's running against one or more documents in a view, or against one document opened in the UI. In either way, the action button in the view or form should simply run the agent using the @Command([ToolsRunMacro]) function.
In the agent, here is some sample code that will determine how the agent should run:
Dim s As New NotesSession
Dim ws As New NotesUIWorkspace
Dim uiDoc As NotesUIDocument
Dim db As NotesDatabase
Dim coll As NotesDocumentCollection
Dim doc As NotesDocument
Set db = s.CurrentDatabase
On Error Resume Next
Set uiDoc = ws.CurrentDocument
If Err <> 0 Then
Err = 0
Set uiDoc = Nothing
End If
If uiDoc Is Nothing Then
Set coll = db.UnprocessedDocuments
If coll.Count = 0 Then
Msgbox "Please choose a document to be processed!!", 16, "None Selected"
Exit Sub
End If
Set doc = coll.GetFirstDocument
Else
Set coll = Nothing
Set doc = uiDoc.Document
End If
While Not doc Is Nothing
' << do your processing here >>
If coll Is Nothing Then
Set doc = Nothing
If uiDoc.EditMode Then Call uiDoc.Reload
If uiDoc.EditMode Then Call uiDoc.Refresh
Else
Set doc = coll.GetNextDocument(doc)
End If
Wend
The agent first tries to go against a document opened in the UI. If there is no document opened, the NotesUIWorkspace method CurrentDocument will return Nothing. (There is an additional check in there just in case it returns an error instead of returning Nothing.) If there isn't a document opened in the UI, then use the UnprocessedDocuments method in the NotesDatabase class to get the list of selected documents in the view. If the user is highlighted on a category, there are no documents to process, so give a message and exit.
Process the selected document using whatever code you want to use. We would recommend making a function call at the point of ' << do your processing here >> in the code above so the structure above can be reused with few changes. Once the document has been processed, if it was opened in the UI then bring any back-end changes into the front end and refresh the document. You can optionally save and close the UI Document if you would like. Then set the looping variable "doc" to Nothing so the inner loop is terminated.
If there were documents selected in the view, continue processing the selected documents until they have all been processed. At the bottom of the code, you could call the method ViewRefresh from the NotesUIWorkspace class to show any updates to the view.