Contact TLCC

Working with Response Documents in LotusScript Click here to see all the Developer/Admin Tips

Date tip published:06/05/2006
Description:The NotesDocument class offers several properties and methods to work with child documents (responses.) This tip covers the Responses property which is used to return all responses to a document, the IsResponse property which is used to determine if a document is a child of another, and the MakeResponse method to create a new response document. This tip includes an attached sample code database.


To learn more about using LotusScript use the following links:

Beginner LotusScript for Notes Domino 7
Intermediate LotusScript for Notes and Domino 7
Advanced LotusScript for Notes Domino 7
Notes Domino 7 LotusScript Package




Working with Response Documents in LotusScript

In Notes/Domino a response document (also known as a child document) is used to create a document hierarchy, or a parent-child relationship between documents. This is very useful for discussion threads or tracking comments. Notes allows for multiple hierarchies where the parent of one document can also be the child document to another.

The NotesDocument class offers several properties and methods to work with child/response documents. This tip covers the Responses property which is used to return all responses to a document, the IsResponse property which is used to determine if a document is a child of another, and the MakeResponse method to create a new response document.




Determining if a Document is a Response Document

The IsResponse property of the NotesDocument class returns TRUE if the document is a response document and FALSE if it is not. The syntax of this property is:

    flag = notesDocument.IsResponse

If the document is a response document then the parent document can be obtained by using the parentDocumentUNID property of NotesDocument. This returns a string which is the UNID of the parent document.
    parentUnid$ = notesDocument.ParentDocumentUNID

To get the parent document use GetDocumentbyUNID as follows:

Dim parentunid as String
Dim parentdoc as NotesDocument
parentunid = currentdoc.ParentDocumentUNID
Set parentdoc = db.getDocumentbyUNID(parentunid)




    Getting all Responses

    The Responses property of NotesDocument class returns a NotesDocumentCollection of all the immediate child/responses to a document. The syntax is:

      Set notesDocumentCollection = notesDocument.Responses

    If there are no response documents then the collection will have zero documents (the count property of the NotesDocumentCollection will be 0.) Only the immediate child/response documents are returned. The Responses property of the immediate response documents can then be used to see if there are more response documents. This is also called a Response to Response type document in the Form properties. A Response to Response type document can be a child of either a Document, a Response, or another Response to Response document. A Response type document can only be a child of a Document.




    Sample Code Database
    Sample Code Database


    The techniques shown in the rest of this tip are illustrated in the attached sample code database. Click on the attachment below to download this database. Put this database on your local Notes data directory and open it via Notes.

    resp.nsf

    To demonstrate the Looping Through All Responses technique, select one document in the view and then click on the [Count Responses] view action.






    Technique - Looping Through All Responses

    If Response to Response documents are in the database and the goal in your script is to process all the responses of a document, then the easiest way is to use a programming technique called recursion. This programming technique uses a subroutine which calls itself from within the subroutine's code. This technique is illustrated in the following code which is in a view action. This code will count all the response documents, not just the immediate responses, to whatever document was selected in a view.

    The code below is in the Click event of the View action. It first gets the selected document (line 12) and than calls a subroutine called countResponses (shown below.) This subroutine increments a global variable (declared in the declarations of the view action) called respcount.

    1.Sub Click(Source As Button)
    2. Dim doc As NotesDocument
    3. Dim colResponses As NotesDocumentCollection
    4. Dim session As New NotesSession
    5. Dim uiView As NotesUIView
    6. Dim uiWS As New NotesUIWorkspace
    7. Set uiView = uiWS.CurrentView
    8. Dim colSelDoc As NotesDocumentCollection
    9. Set colSelDoc = uiView.documents
    10. respcount = 0
    11. If colSelDoc.Count = 1 Then
    12.  Set doc = colSelDoc.GetFirstDocument
    13.  Call countResponses(doc)
    14.  Msgbox "The total responses were " & respcount
    15. Elseif colSelDoc.Count = 0 Then
    16.  Msgbox "Select at least one document by clicking in the selection margin to the left of the document."
    17. Else
    18.  Msgbox "You can only select one document when using this action."
    19. End If
    20.End Sub


    The countResponses subroutine is below. This code uses the Responses property of the selected Notes Document (docToCount) to get the responses to that document on line 4. On line 6 a loop goes through all the immediate responses to the document and increments the counter (respcount.) Line 8 is where the recursion is used. The countResponses subroutine is called again which will check each response document to see if there are responses to the response. This code can do multiple levels of responses by calling itself to check for more responses until no more are found. You can place whatever code you need to process the responses in place of the counter on line 7.

    1.Sub countResponses(docToCount As NotesDocument)
    2. Dim col As NotesDocumentCollection
    3. Dim docResp As NotesDocument
    4. Set  col = docToCount.Responses
    5. Set docResp = col.GetFirstDocument
    6. Do Until docResp Is Nothing
    7.  respcount = respcount + 1
    8.  Call countResponses(docResp)
    9.  Set docResp = col.GetNextDocument(docResp)
    10. Loop
    11.End Sub






      Creating a New Response in LotusScript

      Use the MakeResponse method of the NotesDocument class to make a document a child/response to a another document. This method creates the $REF field that contains the UNID of the desired parent document.

        Call notesDocument.MakeResponse( document )



      Technique - Creating a new Response
      The code below is illustrated in the sample code database. It is an agent set to run from the Actions menu. When this agent is run it will prompt the user for a comment. It next prompts the user to select an existing document that will be the parent. The code then creates a new document in the database and makes this document a child (response) of the selected document. Use the Actions | Make Fast Comment menu option to run this code.

      Line 11 prompts for a comment. Line 15 prompts the user to select a customer from the "Customers" view. This view has the document UNID in column 2. This value is returned from the PickListStrings method. The UNID is used on line 17 to get the desired parent document. Line 19 creates the new document and lines 20 to 26 populate the form, subject, DocAuthor, and CreateDate fields. Line 28 makes this new document a response to the selected document. On line 30 the new document is saved.

      1.Sub Initialize
      2. Dim db As NotesDatabase
      3. Dim session As New NotesSession
      4. Dim uiws As New NotesUIWorkspace
      5. Dim resp As Variant
      6. Set db = session.CurrentDatabase
      7. Dim docResp As NotesDocument
      8. Dim docParent As NotesDocument
      9. Dim comments As String
      10. 'Get input
      11. comments = Inputbox("Enter your comments")
      12. 'Choose which customer to comment on
      13. If comments <> "" Then
      14.  'Get list of customers
      15.  resp = uiws.PickListStrings(PICKLIST_CUSTOM ,False, db.Server,db.FilePath,"Customers","Choose a customer", "Choose a customer for this comment.",2)
      16.  'Get Parent document
      17.  Set docParent = db.GetDocumentByUNID(resp(0))
      18.  'Create new document
      19.  Set docResp = db.CreateDocument()
      20.  'Set form name
      21.  docResp.form = "Comments"
      22.  'Set subject
      23.  docResp.subject = comments
      24.  'set DocAuthor and CreateDate
      25.  docResp.DocAuthor = session.UserName
      26.  docResp.CreateDate = Now
      27.  'Make response
      28.  Call docResp.MakeResponse(docParent)
      29.  'save document
      30.  Call docResp.Save(True,False)
      31. End If
      32.End Sub