Manage Learn to apply best practices and optimize your operations.

Preventing conflicts when updating parent from response

If a user creates a response doc from a parent doc, then edits and saves the parent, a conflict occurs because the parent has been changed. This tip suggests a fix to the problem.

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.
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.

Dig Deeper on LotusScript

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close