Andre C
Andre C

Reputation: 487

Domino Designer - The document is not in view <my view>

I have the following code, which loops through the documents in TestView1, and for each document in that view loops through the documents in TestView2, performing some action (outputs to a file).

For the sake of keeping the loops relatively timely (there's heaps of older documents and I only want those time stamped from today or an upcoming date), I have found the first document for the current date, and stored its UNID so that that document can act as a starting position for the inner loop (the documents in MyView2 are in chronological order).

The inner loop runs correctly the first time, but come the second loop, I get the error message "The document is not in view TestView2". I have added what line breaks my code.

Here is my code:

Dim sess As New NotesSession
Dim db As NotesDatabase
Dim rDoc As NotesDocument
Dim mDoc As NotesDocument
Dim rView As NotesView
Dim mView As NotesView
Dim unid As String
Dim todayDateTime As New NotesDateTime("Today")

Set db = sess.currentDatabase
Set rView = db.GetView("TestView1")
Set rDoc = rView.GetFirstDocument
Set mView = db.GetView("TestView2")
Set mDoc = mView.GetFirstDocument

Set mDoc = mView.GetFirstDocumentb 'Finds the UNID of the first document of the day
Do While Not (mDoc Is Nothing)
    Dim startDate As New NotesDateTime(mDoc.startDate(0))
    If startDate.DateOnly = todayDateTime.DateOnly Then
        unid$ = mDoc.UniversalID
        Exit Do
    End If
    Set mDoc = mView.GetNextDocument(mDoc)
Loop

While Not (rDoc Is Nothing) 'Outer Loop
    If rDoc.Site(0) = "SAMPLE" Then

        <CODE>

        If Not unid$ = "" Then
            Set mDoc = db.Getdocumentbyunid(unid$)
            While Not (mDoc Is Nothing) 'Inner Loop
                If mDoc.ResourceName(0) = rName$ Then

                    <PRINT TO FILE CODE>

                End If
                Set mDoc = mView.GetNextDocument(mDoc) <--- This line here breaks the code**
            Wend
        End If
    End If
    Set rDoc = rView.GetNextDocument(rDoc)
Wend

Would appreciate any help.

Upvotes: 2

Views: 1262

Answers (2)

Knut Herrmann
Knut Herrmann

Reputation: 30960

There is a more efficient approach: categorize your mView by ResourceName + StartDate and use mView.getDocumentByKey(keys, true) to access the first relevant document in mView:

  1. Add a first categorized column ResourceName to mView
  2. Add a second categorized column StartDate to mView
  3. Change your code to:
Dim sess As New NotesSession
Dim db As NotesDatabase
Dim rDoc As NotesDocument
Dim mDoc As NotesDocument
Dim rView As NotesView
Dim mView As NotesView
Dim rName As String
Dim keys(1) As Variant

Set db = sess.currentDatabase
Set rView = db.GetView("TestView1")
Set rDoc = rView.GetFirstDocument
Set mView = db.GetView("TestView2")

While Not (rDoc Is Nothing) 
    If rDoc.Site(0) = "SAMPLE" Then
        rName = rDoc.ResourceName(0)
        ' <CODE>
        keys(0) = rName
        keys(1) = Today
        Set mDoc = mView.GetdocumentbyKey(keys, true)
        While Not (mDoc Is Nothing) 
            If mDoc.ResourceName(0) = rName Then 

                ' <Print To FILE CODE>

                Set mDoc = mView.GetNextDocument(mDoc)
            Else
                Set mDoc = Nothing 
            End if
        Wend
    End If
    Set rDoc = rView.GetNextDocument(rDoc)
Wend

Upvotes: 0

Tode
Tode

Reputation: 12060

The thing is quite easy: When you get a document through its unid, you do NOT get it from the view. That way you will not get a "next" document. You need to "re- get" the document from view to be able to navigate.

Try this:

Dim viewNav as NotesViewNavigator
Set viewNav = mView.CreateViewNav
...
Set mDoc = db.Getdocumentbyunid(unid$)
'- now get the same document, but this time from view:
Set mDoc = viewNav.getEntry( mDoc ).Document

That should help.

Though mDoc stays the same document, this time it is initialized from view and can be used in "GetNextDocument"...

By the way: Using a NotesViewNavigator in my experience is most of the time faster to cycle through a view than to use the "native" NotesView- Methods.

EDIT: Same thing can occur, if you have a NotesDocument that is in a NotesDocumentCollection but not taken directly from there. Then you will get an error "document is not from this collection". There is is the same method: Set doc=collection.GetDocument( doc )

Upvotes: 1

Related Questions