Thursday, April 2, 2009

Document was deleted from this collection

In LotusScript, you might encounter the error number 4434 and/or the error description "Document was deleted from this collection" when looping through a NotesDocumentCollection object and calling the DeleteDocument method.

I'm a big fan of OpenLog, which has shaved off a ton of development time when coding back-end objects that are difficult to debug with the LS debugger. Most recently, it helped detect a bug in my code that might never have been caught. The error wasn't fatal, but it revealed that my code just wasn't doing what I thought it should be doing.

Anyway, since Google is our friend when we run into issues like this, I thought I'd post it to benefit any future searchers. Notes help is fairly useless for troubleshooting error numbers and in this particular case the example code only retrieves the first document in the collection.

Credit for helping me solve this problem goes to Domino Power and Mick Moignard. The article is about folders, but the relevant piece has to do with utilizing a temporary NotesDocument object and setting it to your current NotesDocument object so you can use that object as the parameter in the DeleteDocument method. This allows you to use your current NotesDocument as the parameter in the GetNextDocument method at the end of your loop. Without it, the GetNextDocument has no reference point to refer to in the collection.

Here's an example where you want to remove a document from a collection based on some criteria (SomeFlag = 1) and maybe do something with the collection later on, such as StampAll.

'Assume I have already built the NotesDocumentCollection called docCol
'and I have dimensioned two NotesDocument objects, curDoc & tmpDoc
Set curDoc = docCol.GetFirstDocument
For i = 1 to docCol.Count
        If curDoc.SomeFlag(0) = 1 Then
                Set tmpDoc = curDoc
                Call docCol.DeleteDocument(tmpDoc)
        End If
        Set curDoc = docCol.GetNextDocument(curDoc)
Next

Using the temporary document allows you to not lose your reference to the current document in your collection.

2 comments:

Anonymous said...

The correct way:

Dim view As NotesView
Dim coll As NotesDocumentCollection
Dim doc As NotesDocument
Dim nextdoc As NotesDocument
While Not doc Is Nothing
Set nextdoc = view.GetNextDocument(doc) ' or Set nextdoc = coll.GetNextDocument(doc)
' code, for example : Call doc.Remove(True)
Set doc = nextdoc ' new doc is nextdoc
Wend

Mike Miller said...

Anonymous,

Correct way if you're deleting the document from the database. Deleting first and then trying to find the next document by referring to the current deleted document would be difficult.

In my example I'm deleting the document from the document collection, so I still have a reference to the document when I go to get the next one.