Updating Response documents when parent data changes
This tip describes updating Response documents when parent data changes.
Occassionally I develop applications that require response documents to maintain certain data contained within the parent document. When certain data in the parent document changes, all response documents also need to change.
The following code/script helps accomplish that task. The parent document maintains a list of field names that are inherited by the response document. When the parent document is saved, that list is used to determine which fields have changed. If any data has changed, the immediate responses are updated with the new data. NOTE: This code only updates immediate reponses. It could be modified to handle responses to responses.
- Create a hidden field called "InheritedFields" on the parent form that is multivalue text. The value should be a list of all field names that need to be updated on child document when updates are made to parent(e.g. "Field1" : "Field2" : "Field3").
- In the Declarations of the parent form, include the following:
'handle to parent document Dim doc As NotesDocument 'two dimensional array containing field name and field value Dim inheritFieldsArray As Variant
- In the QueryModeChange of the parent form:
Dim listOfInheritedFields As Variant Dim doc As NotesDocument
'when document enters edit mode, capture original values to compare during save If Not(source.IsNewDoc) And Not(source.EditMode) Then Set doc = Source.Document listOfInheritedFields = doc.InheritedFields Redim inheritFieldsArray(0 To Ubound(listOfInheritedFields), 0 To 1) For i=0 To Ubound(listOfInheritedFields) inheritFieldsArray(i, 0) = listOfInheritedFields(i) inheritFieldsArray(i, 1) = Source.FieldGetText(listOfInheritedFields(i)) Next End If - Create UpdateChildDocuments sub routine:
Sub UpdateChildDocuments Dim actDC As NotesDocumentCollection Dim actDoc As NotesDocument Stop Set actDC = doc.Responses If actDC.Count > 0 Then Dim fieldUpdateArray() As String Dim fieldUpdateCount As Integer fieldUpdateCount = 0 'initialize array to "" Redim fieldUpdateArray(0 To 0) fieldUpdateArray(0) = "" 'there are activities for this document--continue checking to see if updates are necessary For i=0 To Ubound(inheritFieldsArray) fieldName = inheritFieldsArray(i,0) origFieldVal = inheritFieldsArray(i,1) currFieldVal = doc.GetItemValue(fieldName)(0) If (origFieldVal <> currFieldVal) Then 'the value has changed--flag for update Redim Preserve fieldUpdateArray(0 To fieldUpdateCount) fieldUpdateArray(fieldUpdateCount) = fieldName fieldUpdateCount = fieldUpdateCount + 1 End If Next If fieldUpdateArray(0) <> "" Then 'updates need to be performed Set actDoc = actDC.GetFirstDocument While Not (actDoc Is Nothing) 'for each activity document, replace the changed values For i=0 To Ubound(fieldUpdateArray) currVal = doc.GetItemValue(fieldUpdateArray(i))(0) Call actDoc.ReplaceItemValue(fieldUpdateArray(i), currVal) Next Call actDoc.Save(True, True) Set actDoc = actDC.GetNextDocument(actDoc) Wend End If End If End Sub
- In the QuerySave of parent document:
Set doc = Source.Document 'update any child documents if values have changed If Not (Source.IsNewDoc) Then UpdateChildDocuments End If