Deleting Orphans

Hello,

I have a database where completed documents are eventually sent to an archive database. When this happens, the documents’ responses become orphans. So I wrote a “Delete Orphans” agent that looks like this:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Set db=session.currentdatabase

Dim doc As NotesDocument

Dim parentdoc As NotesDocument

Dim dc As NotesDocumentCollection

Set dc = db.AllDocuments

For j = 1 To dc.Count

Set doc = dc.GetNthDocument(j)

If doc.IsResponse = True Then

  Set parentdoc = db.GetDocumentByUNID(doc.ParentDocumentUNID)

  On Error Resume Next

  If (parentdoc.Responses Is Nothing) Then

    Call doc.Remove(True)

  End If

End If

Next j

End Sub

Since the parent document was deleted, I was hoping to satisfy the “Is Nothing” condition. Instead, the previous document’s parent is still there. Or, if the first document is an orphan, I get an “Invalid universal ID” message.

This agent used to work and now it doesn’t. The only thing I could possibly have changed is the “Soft Deletion” option.

Can someone tell me how to make this work?

Thanks

Subject: Here is an archiving routine I use…

I use this to archive a document and its responses:

Sub ArchiveDocument(doc As NotesDocument, dbArch As NotesDatabase)

%REM

This routine will archive the document that is passed to this routine.

It will archive/move the document as well as any responses to the 

archive database that is passed to this routine.

The only thing it does not do is remove the doc that is passed.

That should be done from where this routine is being called.

It does, however, remove the responses, if there are any.

%END REM

On Error Goto ProcessError



Dim s As NotesSession   	

Dim db As NotesDatabase

Dim dcResp As NotesDocumentCollection

Dim docArch As NotesDocument

Dim docResp As NotesDocument

Dim docArchResp As NotesDocument

Dim Message As String

Dim Continue As Variant

Dim y As Integer



Set s = New NotesSession

Set db = s.CurrentDatabase



'Let's copy the doc to the Archive...

Set docArch = dbArch.CreateDocument



'Now, lets' copy all the items...

Call doc.CopyAllItems(docArch, True)



'Now, let's save the Archived document...

Call docArch.Save(True, True)



'Now, let's get all the responses (if any) from the main document...

Set dcResp = doc.Responses

If dcResp Is Nothing Then

Else		

	y = 0

	For y = 1 To dcResp.Count

		If y = 1 Then

			Set docResp = dcResp.GetFirstDocument

		Else

			Set docResp = dcResp.GetNextDocument(docResp)

		End If

		If docResp Is Nothing Then

		Else				

			Set docArchResp = dbArch.CreateDocument

			Call docResp.CopyAllItems(docArchResp, True)

			Call docArchResp.MakeResponse(docArch)

			Call docArchResp.Save(True, True)						

		End If

	Next

	'Let's remove all the responses...

	Call dcResp.RemoveAll(True)

End If	



Exit Sub	

ProcessError:

Continue = False

Message = "Error (" & Cstr(Err) & "): " & Error$ & " on line " & Cstr(Erl) & " in ArchivingRoutines: ArchiveDocument."

Messagebox Message, 16, Error In Processing…”

Exit Sub

End Sub

I pass the document to be archived and the database to archive to. It works pretty well for me.

Sample call:

	'Copy document to the archive DB...

	Call ArchiveDocument(doc, dbArch)	

HTH,

Dan

Subject: Deleting Orphans

on error goto errhandler

For j = 1 To dc.Count

Set doc = dc.GetNthDocument(j)

If doc.IsResponse = True Then

Set parentdoc = db.GetDocumentByUNID(doc.ParentDocumentUNID)

If (parentdoc is nothing)or (parentdoc.Responses Is Nothing) Then

Call doc.Remove(True)

End If

End If

Next j

exit sub

errhandler:

set parentdoc = nothing

resume next

HTH

Sriram

Subject: Deleting Orphans

The soft deletion might have a lot to do with it, since a soft-deleted parent document does still exist in the database, so it can still be a legal parent. It probably it better not to remove the responses of a soft deleted document anyway, since if the document is restored you’ll wan the responses to still be there.

A true deleted document also exists in the database as a deletion stub. If you ask for the UNID, there is such a document. The IsDeleted property might help you there.