Q
Problem solve Get help with specific problems with your technologies, process and projects.

How to make computed fields recalculate

When Notes/Domino developers say they want to "update the fields" in tha Lotus Notes view, they usually mean that they want to cause computed fields to recalculate. SearchDomino.com development expert Andre Guirard explains the process and offers some bonus words of wisdom on LotusScript programming.

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

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close