I have a Lotus Notes view, and I want to save each of the documents in the Lotus Notes view in order to update the fields using an agent and the following LotusScript code:
Sub Initialize ' instantiate the notessession object Dim s As New NotesSession ' declare object variables Dim db As NotesDatabase Dim v As NotesView Dim doc As NotesDocument ' instantiate objects Set db = s.CurrentDatabase Set v = db.GetView("V_ENG") Set doc = v.GetFirstDocument Do Until doc Is Nothing ' save the doc Call doc.Save( False, False ) Set doc = v.GetNextDocument(doc) Loop ' refresh the view Call v.Refresh End Sub
I am really stuck on this, so any help or advice would be greatly appreciated.
When Notes/Domino developers say they want to "update the fields" in a Lotus Notes view, they usually mean that they want to cause computed fields to recalculate. Assuming this is what you have in mind, you don't need an agent -- you can do it by creating a toolbar icon or view action that uses the formula @Command([ToolsRefreshSelectedDocs]). Select the documents you want to update and click this icon.
Although the LotusScript was unnecessary in this case, it does give me an excuse to lecture about useful principles of LotusScript programming, so listen up.
First, you should know that the back-end methods (NotesDocument.xxx) pay no attention to the form whatsoever. Using LotusScript or even a Formula agent, you can assign any field with any value -- even the totally wrong datatype, or even make up and assign fields that are not even on the form. Lotus Notes will not complain, nor will it change the values of any other fields that you did not explicitly set, even if they are computed fields based on the field you changed. The form is only used when you edit the document on-screen -- at other times it is ignored (with one exception I describe below).
The nice part is that this gives you total control; the downside is that sometimes you want these things done for you!
To get the fields to compute when you're not editing the document, use the NotesDocument.ComputeWithForm method. You can call this method to compute all the computed field formulas, input translations and validations, and update the document accordingly. Take care using this in a server agent, however, because it may fail for formulas that assume they're running on a Lotus Notes client (e.g., that use @UserName or try to @DBLookup in databases on a different server (which would require the current server to be "trusted" by the remote server) .
Also, if you're updating documents in a view, it's best to use view.AutoUpdate = False to prevent the view being refreshed every time you save a document. Not only is this slow, it can make you lose track of your position in the view. It will make the document you were just working on move to a different spot in the view, because of the changes you made to its fields.
In addition, any time you write a LotusScript agent that handles a lot of documents, use the Delete statement to remove documents from memory after you're done with them. Otherwise, you're filling up your available memory with cached documents.
So the resulting code would look something like this:
Sub Initialize Dim s As New NotesSession ' declare object variables Dim db As NotesDatabase Dim v As NotesView Dim doc As NotesDocument Dim docNext As NotesDocument ' instantiate objects Set db = s.CurrentDatabase Set v = db.GetView("V_ENG") v.AutoUpdate = false Set doc = v.GetFirstDocument Do Until doc Is Nothing set docNext = v.GetNextDocument(doc) If doc.ComputeWithForm (True, False) Then Call doc.Save( False, False ) Else ' a validation formula failed; I don't know what you would want to do here. End If Delete doc ' remove doc from cache (does not delete from database) Set doc = docNext Loop ' refresh the view (don't bother) ' Call v.Refresh End Sub
Do you have comments on this Ask the Expert Q&A? Let us know.
Related information from SearchDomino.com:
Dig Deeper on Lotus Notes Domino Formula Language
Related Q&A from Andre Guirard
Learn how you can use LotusScript and OLE to create and populate Microsoft Excel spreadsheets, as well as a little bit about Lotus Symphony. Continue Reading
Discover options you can use if you'd like external users to be able to access a workflow-based Notes Domino application through different ... Continue Reading
Learn why you may receive the error message: "Illegal circular use: Audit Trail" when trying to open Lotus Notes documents that use copied forms. Continue Reading