How do I remove a response document from a view in script?

Afternoon all,

I have an agent which is called from an action button on a view. The agent deletes the currently selected document from the view. If the document has responses they are deleted too, if not, or the selected document is a response document, then only the selected document is deleted.

The problem seems to be that although I can see the response documents ID in a hidden view I can’t assign it to a notesdocument variable to be removed.

I have added my code below. When the code runs I get a ‘variable not set’ error on the ‘deleteDoc.Remove (True)’ line.

My hidden view has just one column which displays the value of the ‘documentUniqueID’ field which is stored in each document. I have ticked the “Show response documents in a hierarchy” box. My view selection formula is as follow: Select Form=“MyForm”.

Can anyone tell me why this is not working?

Thanks in advance,

    Dim session As NotesSession

    Dim db As NotesDatabase

    Dim view As NotesView

    Dim selectedDoc As NotesDocument

Dim collection As NotesDocumentCollection

Dim responseDoc As NotesDocument

Dim deleteDoc As NotesDocument

Dim j As Integer



Set session = New NotesSession

Set db = session.CurrentDatabase

Set view = db.getView("vewDocumentsByUID")

Set selectedDoc = session.DocumentContext

Set collection = selectedDoc.Responses

Set deleteDoc = view.getDocumentByKey(selectedDoc.documentUniqueID(0))



If (collection.Count = 0) Then

	Call deleteDoc.Remove( True )

Else

	For j = 1 To collection.Count 

		Set responseDoc = collection.GetNthDocument( j ) 

		

		Call responseDoc.Remove( True )

	Next 

	

	Call deleteDoc.Remove( True )

End If



Set deleteDoc = Nothing

Set responseDoc = Nothing	

Set collection = Nothing

Set selectedDoc = Nothing

Set view = Nothing

Set db = Nothing

Set session = Nothing

Subject: Why are you bothering to refind the doc in another view?

Dim s As NotesSessionDim db As NotesDatabase

Dim dc As NotesDocumentCollection

Dim doc As NotesDocument

Set dc = db.UnprocessedDocuments

For i = dc.Count To 1 Step -1

Set doc = dc.GetNthDocument(i)

Call DeleteDocAndResponses(doc)

Next

Sub DeleteDocAndResponses(d As NotesDocument)

Dim cdc As NotesDocumentCollection

Dim cdoc As NotesDocument

Set cdc = d.Responses

For k = cdc.Count To 1 Step -1

Set cdoc = cdc.GetNthDOcument(k)

Call DeleteDocAndResponses(cdoc)

Next

Call d.Remove(True)

End Sub

Subject: Way too complex

This should work whether or not the responses show up in the view the user runs the agent from. Actually, if the responses are visible and selected, that might be a problem.

Dim s As NotesSession

Dim db As NotesDatabase

Dim dc As NotesDocumentCollection

Dim doc As NotesDocument

dim docdup as notesdocument

Set dc = db.UnprocessedDocuments

Set doc = dc.getfirstdocument

do until doc is nothing

'first remove all responses, if any

forall r in doc.responses

r.remove true

end forall

'then remove the selected doc

set docdup = doc 'need this copy to get the next doc in the collection

set doc = dc.getnextdocument(doc)

docdup.remove true

loop

Subject: RE: Way too complex

Alex,

As you are running the code from the view action button, All the selected documents will be processed by this agent leaving the other documents in the view by simply using the db.UnprocessedDocuments, as mentioned by Bruce.

So now you get the collection opf the selected. Loop thru the collection and process them (in your case delete). If you want to remove responses also, check if there are any responses for that document in loop, with the property “doc.Reponses” and get them in a “respcol” and simply use

Call respcol.RemoveAll(True) to remove them and then delete the mail document by this method Call doc.Remove(True).

Hope this helps.

Subject: RE: Way too complex

I always forget to use notesdocumentcollection.removeall and .stampall! I have some sort of mental block where those methods are concerned. So you’re right, Narasimhan, if Alex replaces the forall loop in my code with

doc.responses.removeall true

then that would be more efficient.

Subject: RE: Way too complex

Thanks Bruce.

Subject: Thats fine unless there are Response to Response documents.

Subject: D’oh! I forgot about recursing!

Guess that rules out removeall after all. (I’ll pretend I meant to do that.)

This should do. This would DEFINITELY have to run against ONLY main docs, not any of the responses it ultimately removes.

Sub KillAllResponses(doc)

dim rcol as notesdocumentcollection

set rcol = doc.responses

if rcol.count = 0 then exit sub

forall r in rcol

KillAllResponses r

remove r, true

end forall

End Sub

Sub Initialize()

Dim s As New NotesSession

Dim db As NotesDatabase

Dim dc As NotesDocumentCollection

Dim doc As NotesDocument

dim docdup as notesdocument

set db = s.currentdatabase

Set dc = db.UnprocessedDocuments

Set doc = dc.getfirstdocument

do until doc is nothing

set docdup = doc 'need this copy to get the next doc in the collection

KillAllResponses doc

set doc = dc.getnextdocument(doc)

docdup.remove true

loop

end sub