Tech Tip: When NotesDocument.Responses is populated, when it's not, and how to work around it
Category Technical Lotus
Bookmark :
Yesterday my biggest fan, Betsy Thiede, contacted me via BleedYellow's Sametime service (which is friggin' AWESOME - thanks Lotus 911!) to ask me about a technical problem she was having with a particular form's LotusScript. Here's the problem.
The Problem
Her form had some code in the QuerySave and QueryClose event that checked the NotesDocument.Responses property to see if there was a response; this particular form required a response be created for it as well, before it is saved and closed. However the NotesDocument.Responses property wasn't populated the first time the document was created and saved in the QueryClose event. Remember, if the NotesDocument hasn't been saved (i.e. NotesDocument.IsNewNote = FALSE) then there are certain properties that won't be initialized, including the NotesDocument.Responses property. When the NotesDocument object is new, the Responses property is NOTHING, so there is NotesDocumentCollection object available in that property.
So, let me restate the sequence, to make it clear:
- The form is loaded to create a new document.
- The user fills out the form, then either clicks the "Save & Close" button (which contains simple @formula code to execute the Save and Close), or hits ESC to get prompted to save the document.
- The document has some code in the QueryClose event; this code fires, but isn't related to this problem.
- The document has code in the QueryClose event that performs the check of the NotesDocument.Responses property for the newly saved document.
- NOTE: If you put code in the PostSave event to check the following properties, the results are as follows:
- If you check the Source.IsNewDoc property, it returns FALSE, which confirms that, in fact, the document was successfully saved.
- If you check the Source.Document.IsNewNote property, it returns TRUE - which means that the back-end document doesn't think it has been saved
- Because the back-end document doesn't know it is saved, the NotesDocument.Responses property is not populated - it isn't populated until the document has been saved, and it doesn't know it is saved for some strange reason
I find it unusual that the Source (which is a NotesUIDocument object) object knows it is saved (Source.IsNewDoc), but the related Document object (Source.Document is a NotesDocument object) doesn't know it is saved (Source.Document.IsNewNote). Weird, huh?
The Solution
Rather than trying to figure out why Notes is doing this, I decided to take another tact - convince the NotesDocument object that it has, in fact, been saved. We simply added an additional save call to the PostSave event - Call Source.Document.Save(True, False). By explicitly calling a back-end save on the NotesDocument object, it convinced the NotesDocument object that it has been saved, and this in turn caused the NotesDocument.Responses property to be properly populated (wow, alliteration, and I wasn't even trying
By adding this extra save Betsy can now check the Source.Document.Responses.Count to see if it is greater than zero, and perform her validations, etc.
Incidentally, she can also check to see if the Responses property is populated (Source.Document.Responses is Nothing), or she can even check the Source.Document.IsNewNote to see if it is TRUE, because if either of these is the case, then the Responses property will not be populated, and therefore there won't be any responses.
But, since she already had code written to test this property, she decided to simply add the save call to ensure the Responses property is populated.
Rock









Blog Roll












Comments
As usual, I am grateful to you.
Posted by Betsy Thiede At 01:26:48 PM On 04/23/2008 | - Website - |
That's a great solution, but I don't understand why do we have to check the Responses of the current document which is not even saved ?? or which is been just created.
Cheers
Karthik
Posted by Karthik At 06:39:00 AM On 05/09/2008 | - Website - |
the disadvantage of the said solution is that it takes extra time to save the back-end document.
In a test with a 4 MB attachment I measured 670 msec extra time.
an other solution could be:
1.)
Set currentNote = Source.Document
If currentNote.IsNewNote Then
Set currentDatabase = currentNote.ParentDatabase
strDocunid = currentNote.UniversalID
Set currentNote = currentDatabase.GetDocumentByUNID(strDocunid)
End If
2.) - this is my favourite:
Set currentNote = Source.Document
If currentNote.IsNewNote Then
Delete currentNote
Set currentNote = Source.Document
End If
Regards Reinhard
Posted by Reinhard At 05:07:49 AM On 05/27/2008 | - Website - |