« Lotus Notes Sighting: CSI's Grissom uses Lotus Notes! | Main| Lotus Geek Honor Roll becomes Lotus Geek Blog Roll »

Tech Tip: When NotesDocument.Responses is populated, when it's not, and how to work around it

QuickImage   
Category  
Bookmark : del.icio.us  Technorati  Digg This  Add To Furl  Add To YahooMyWeb  Add To Reddit  Add To NewsVine 

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

Comments

1 - Thanks Rocky for helping me.. The resolution worked, and I am happy with that.. Still nagging me a bit. But sometimes have to move on..

As usual, I am grateful to you.

2 - Hi Rock,

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

3 - Hello Rock,
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

Meet Rocky

Rocky Oliver
Rocky Oliver
If you see me at a conference, please stop me and say hi!

Calendar

Search

Categories

LotusGeek Tour 2008

ILUG2008Small.png

Proudly Employed By

I am the Vice President of Products for TeamStudio

Our Corporate Blog

I am the Vice President of Products for TeamStudio

Thawte Notary

Thawte Web of Trust Notary

LOTUS GEEK gear

Social Networking


Add to Technorati Favorites

View Rocky Oliver's profile on LinkedIn

Rocky  Oliver

Dilbert

Buy my book!

Blog Buttons

Atheist - Unitarian - Humanist

Atheist Symbol

chalice_150.gif

Happy Humanist

Poker Players Alliance

My Blogmap


Pil Sung!

I am a first-degree black belt (il Dan) in Choi Kwang Do. Pil Sung!

This Site Designed By

YOU! If you would like to see your name and link here, read more about the Skin the Geek contest!