Agent to delete documents

I am receiving an error on the agent below on this line: “Dim collection As NotesDocumentCollection”. I cannot get debugger to work for some reason. I am not versed well on LS. Could someone take a look at this and provide any suggestions? Thanks!OR if there is an easier way to delete duplicate documents I am open to that as well :slight_smile:

(The purpose of this agent is to delete duplicate documents based on a view that I created).

Option Public

Option Declare

Sub Initialize

Dim s As NotesSession

Dim selection As String

Dim collection

Dim collection As NotesDocumentCollection

Dim doc1 As NotesDocument

Dim doc2 As NotesDocument

Set db = s.CurrentDatabase

Dim view As NotesView

Dim count As Integer

Set view = db.GetView(“vPMISTask”)

Set doc1 = view.getfirstdocument

While Not (doc1 Is Nothing)

         Set doc2 = view.GetNextDocument(doc1) 

If Not (doc2 Is Nothing) Then

If (doc1.PMISTask(0) = doc2.PMISTask(0)) Then

'----Mark Delete document to 1------------

doc2.Mark = "1"

Call doc2.save(True,True)

End If

End If

Set doc1 = view.GetNextDocument(doc1)

Wend

'------Delete Document

        Dim dateTime As New NotesDateTime( "" )

Selection = “(Form = ““FORM_NAME”” & Mark=”“1"” )"

Set collection = db.Search( Selection, dateTime, 0 )

For j = 1 To collection.Count

Set doc2 = collection.GetNthDocument( j )

                    Call doc2.remove(True) 

Next

End Sub

Subject: Agent to delete documents

take out the line that says Dim collection

Subject: RE: Agent to delete documents

When I take out that line I now get errors on the following lines:

Set db = s.CurrentDatabase

Set doc1 = view.getfirstdocument

For j = 1 To collection.Count

Subject: Agent to delete documents

Don’t use GetNthDocument…

What I do, especially if the number of documenst to delete is not extremely large, is to build a list of NotesDocumenst or a list of strings (containing the UNID for the documents to delete).

Something like this:

Dim deletedoc List As NotesDocument

Set view = db.GetView(“vPMISTask”)

Set doc1 = view.GetFirstDocument

Do Until doc1 Is Nothing

Set doc2 = view.GetNextDocument(doc1)

If doc2 Is Nothing Then

Set doc1 = Nothing

Else

If (doc1.GetItemValue(“PMISTask”)(0) = doc2.GetItemValue(“PMISTask”)(0)) Then

'*** doc2 should now be deleted

Set deletedoc(doc2.UniversalID) = doc2

Set doc1 = view.GetNextDocument(doc2)

End If

Loop

ForAll ddoc in deletedoc

Call ddoc.Remove(True)

End ForAll

Assuming I understood your logic.

Subject: Agent to delete documents

Try changing your first line to:

Dim s as New NotesSession

in addtion to removing the line that Barry suggested.

Subject: RE: Agent to delete documents

When I do both of those I now get an error on this line:For j = 1 To collection.Count

Subject: RE: Agent to delete documents

Since you used Option Declare you need to declare all variables.

So add

Dim J as long

Subject: RE: Agent to delete documents

That worked! Thanks much everyone!!

Subject: Agent to delete documents

Since you used Option Declare you need to declare all variables.

So add

Dim J as long