API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Refresh Parent When Closing Child
During development of a new application, we ran across a situation that I'm sure some of you have seen. We have a parent document and children documents. The children are only displayed in an embedded view in the parent document. So, when you open up the parent, you see an embedded view with "show single category" enabled that shows all the children of that parent. Those children don't show up in any other view, so you must open up the parent to see them. There's buttons in the parent to create a child, edit a child, delete a child, etc.

To make things more user-friendly, instead of showing an empty embedded view, if there are no children then the embedded view is hidden (there's a hide-when on the paragraph showing the embedded view) and another line of text saying something like "there are no children" is shown instead. This is where the 'problem' arises. When you click on the button to create the first child, once that child is saved there needs to be a way to update that parent so the embedded view will be shown and the other line will be hidden.

Of course, pressing F9 when you return to the parent after saving the first child will be sufficient because that will refresh the document, including the hide formulas. (That assumes that your view index has been refreshed and your lookups are not caching too much information). But how do you refresh the parent document automatically when you're editing the child document?

After thinking about it for a while, I ended up implementing something that works pretty well. Keep in mind that the only way to create/edit a child document (in this application) is inside the parent (through buttons on the parent). So I decided to work with the QueryClose event on the child. If the child is being edited, then re-open the parent. This allows the child to establish a handle to the parent. Using that handle, the parent can be refreshed. What you have to make sure, when re-opening the parent, is that you don't open up a new window, but instead open up the window that should already be open. (Of course, there's nothing stopping the end user from closing that parent while editing the child, but that should be a rare occurrence).

So, here's the code in the QueryClose even of the child:

Sub Queryclose(Source As Notesuidocument, Continue As Variant)
   If Source.EditMode = True Then
      Dim session As New NotesSession
      Dim ws As New NotesUIWorkspace
      Dim db As NotesDatabase
      Dim doc As NotesDocument
      Dim uiDoc As NotesUIDocument
      Set db = session.CurrentDatabase
      Set doc = db.GetDocumentByUNID(Source.Document.ParentDocumentUNID)
      Set uiDoc = ws.EditDocument(True, doc, False, "", False, False)
      Call uiDoc.Refresh
   End If
End Sub

On the EditDocument method, the final False is the "New Instance" parameter. Since it's False, that tells Notes to re-use the existing window if it's available. If it's not available, it will open up a new window. Also, notice that the handle to the uiDocument is returned (we save the return value). We can take that handle and call the Refresh method, which updates the lookups and hide formulas, so the embedded view is shown instead of the "No Children" message.

Naturally, this isn't a necessity - you could not use the hide formulas and just have an empty embedded view if there's no children. But adding this little bit of code makes for a better user experience.