Reg Exp
Web Design
Notes Client
Caching when updating responses
A friend of mine has an application that has documents in a parent-child relationship. The child documents have values that are computed based on the parent. When the parent changes, those children need to be updated. After the database grew quite a bit, the updating of the responses wasn't working any more.

Let me first give a little background. The response documents had computed fields with a formula like:
@GetDocField(@Text($Ref); "FieldName"))
(where "FieldName" was the name of the field in the parent document). When a value in the parent document would change, that value needed to be propagated to the children. Since there were several fields that looked up to the parent, it wasn't economical to list out all the fields and update each one individually. Instead, the ComputeWithForm method was used to recompute all the fields.

Initially the code worked great. It looked something like this:

Set doc = uiDoc.Document
Set children = doc.Responses
Set child = children.GetFirstDocument
While (Not child Is Nothing)
   Call child.ComputeWithForm(True, False)
   Call child.Save(True, False, True)
   Set child = children.GetNextDocument(child)

All the children would be refreshed and the updates in the parent document were reflected in the children documents.

After a while, though, that code didn't seem to be reliable. The old values in the parent were being saved in the children until a child document was opened up and refreshed in the Notes client. Since the old values were being used (and not some error message or a blank value or something), I decided to make a code change that would force the LotusScript to not use a cached version of the parent document. Even though in debugger you could see that the new values were in there, the children were still getting the cached values.

In order to force LotusScript to not use the cached version, you need to get a handle to the document through a different way. Here's a snippet of the updated code:

Set doc = uiDoc.Document
unid = doc.UniversalID
Set doc = Nothing
Set uiDoc = Nothing
Set doc = db.GetDocumentByUNID(unid)
Set children = doc.Responses

The rest of the code is the same (going through the children individually and calling a ComputeWithForm on each child). In this code, I save the UNID of the document in a temporary variable, and then I wipe out the handles to both the back end document and the front end document. The code has no memory of those objects. Then I get the document by going through the current database. To the script, this is not the same document that's currently opened in the UI - it's some other document. Now the compute with form doesn't use any cached version of the document and the fields are recomputed correctly.

Am I telling you that you should use the second version of the code always? Heavens no. I'm just saying that there are multiple ways to achieve the same result in Notes. When one way doesn't work, there's almost always another way to achieve the same result. In my friend's case, the second way allowed the code to do what it was supposed to do after the initial way stopped working.