Script to Remove Duplicates (Works... sort of)

I am using the following script to remove duplicate entries. It works on most Dbs but not some. The failures seem to be in Dbs that have more than 2 duplicates (e.g. 3-6). If I attempt to run the agent on one of the Dbs with > 2 documents, it seems to run for about 3 secs. then stops -w- no errors, etc. Is a doc3… needed? I’m using this to compliment a script that seems to be producing duplicates.

Sub Initialize

Dim s As New notessession

Dim db As notesdatabase

Set db =s.currentdatabase

Set people = db.getview("CustomContacts")

Dim doc1 As notesdocument

Dim doc2 As notesdocument

On Error Resume Next

Set doc1=people.getfirstdocument

Do While Not (doc1 Is Nothing)

	Set doc2=people.getnextdocument(doc1)

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

		doc1.remove True

	End If

	Set doc1=doc2

Loop 

End Sub

Subject: Script to Remove Duplicates (Works… sort of)

This should work on any number of dupes. I would, however, modify the code to eliminate any values that appear the same but are slightly different (extra spaces, case differences, etc.).

Modify to:

If trim(ucase(doc1.CompanyName(0))) = trim(ucase(doc2.CompanyName(0))) Then

Of course, running the script in debugger could also show you where it’s exiting.

Subject: RE: Script to Remove Duplicates (Works… sort of)

Thanks… that is what I thought. It worked on most. Will incorporate your trim as well. Thanks…

Subject: Script to Remove Duplicates (Works… sort of)

Like J Ames says, it should work on any number of documents you check.

You should check if doc2 exists though, otherwise the last document will be deleted (because of the on error resume next)

So:

Set doc1=people.getfirstdocument

Do While Not (doc1 Is Nothing)

Set doc2=people.getnextdocument(doc1)

if doc2 is nothing then

'no more documents, end of view

exit do

end if

If trim(Ucase(doc1.CompanyName(0))) = trim(ucase(doc2.CompanyName(0))) Then

doc1.remove True

End If

Set doc1=doc2

Loop

And the reason you are not getting an error anyway is because the “on error resume next”. Because of this the code will continue on the next line without prompting an error.

So the best way to see if the code is working for you is to remove/disable the on error line and run the code with the script debugger.

Regards,

René

Subject: RE: Script to Remove Duplicates (Works… sort of)

Thanks all… have updated and will test.

Subject: RE: Script to Remove Duplicates (Works… sort of)

Hi Michael,

try this…

http://www-10.lotus.com/ldd/nd6forum.nsf/DateAllFlatweb/b8bf4454504926f1852572c700408074?OpenDocument