I have an application where a response document updates fields in a parent document when it is first saved. The problem, as many of you may know, is that if the user creates the response document from the parent document, then edits and saves the parent document, a conflict occurs because the backend parent document has changed. The NotesUIDocument.reload method does not work in this situation.
I searched the common forums, but could not find any working solutions. Several suggested closing the parent first, but that is harder than it sounds for a variety of reasons. Finally, I came up with the code below, which does the trick and has lots of other little gems for this type of functionality.
Code
Create an action on the parent document to create the response document and only allow the response to be created through this action. Put the following code in the action (note that this does not actually create a document -- it only sets a flag that will be detected in the QueryClose event):
FIELD CreateResponse :=
CreateResponse;
@If(@IsDocBeingEdited; "";
@Command([EditDocument]));
@If(
@IsValid;
@Do(
@SetField("CreateResponse"; "Y");
@PostedCommand([FileSave]);
@PostedCommand([FileCloseWindow])
);
"");
Then place the following code in the QueryClose event of the parent form, which will create the response when the parent is closed:
Sub Queryclose(Source As
Notesuidocument, Continue As Variant)
Dim uiwork As New notesuiworkspace
Dim doc As NotesDocument
Set doc = source.Document
If source.EditMode Then
'Reload to be sure to get the flag
'Not sure if this is necessary
Call Source.Reload
End If
If doc.CreateResponse(0) = "Y" Then
Call uiwork.ComposeDocument
( "", "", "Response" )
'Clear the flag so it doesn't do it
the next time the document is closed.
doc.CreateResponse = ""
Call doc.Save( False, False )
End If
End Sub
If you want to reopen the parent when the response is saved (so the user never really knows it was closed), you can add the following code to the QuerySave and QueryClose events in the response document:
Sub Querysave(Source As
Notesuidocument, Continue
As Variant)
'Open parent in edit mode
If source.IsNewDoc Then
doc.GetParent = "Y"
Else
doc.GetParent = ""
End If
End Sub
Sub Queryclose(Source As
Notesuidocument, Continue
As Variant)
Dim session As New notessession
Dim db As notesdatabase
Dim uiwork As New notesuiworkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim parent As notesdocument
Set doc = Source.Document
Set db = session.CurrentDatabase
If doc.GetParent(0) = "Y" Then
Set parent = db.GetDocumentByUNID
( doc.ParentDocumentUNID )
If Not (parent Is Nothing) Then
Set uidoc = uiwork.EditDocument
( False, parent )
End If
doc.GetParent = ""
Call doc.Save( False, False )
End If
End Sub
Do you have comments on this tip? Let us know.