Copy, updated and delete documents from one to another db

Hi, I use the following code to copy changed and new documents from one to another database.

Now I want to modify the code in the way , that deleted documents in the source-database are deleted in the other database, too.

What must I change? Every help is welcome.

Stefanie


Sub Initialize

Dim db As NotesDatabase

Dim session As New NotesSession

Dim view As NotesView

Dim doc As NotesDocument

Dim destdoc As NotesDocument



dbserver$ = "SYS0099N/aswo" 'Destination Server

dbpath$ = "herstellerEasi.nsf" 'Destination DB

sourceview$ = "Nach Kategorie" 'Source View



Set db = session.CurrentDatabase

Dim destdb As New NotesDatabase(dbserver$, dbpath$)

If destdb Is Nothing Then

	Print "Database not found..."

	Exit Sub

End If

Set view = db.GetView(sourceview$)

Call view.Refresh 'optional



Set doc = view.GetFirstDocument 

Print "Copy documents to " & dbserver$ &" (" & dbpath$ & ")"



While Not (doc Is Nothing) 

	On Error Resume Next

	Set destdoc = destDb.GetDocumentByUnid(doc.UniversalID)

	

	ExistingDoc = False

	If Not destdoc Is Nothing Then

		ExistingDoc=True

		Forall rItem In destdoc.items

			Call destdoc.RemoveItem(rItem.Name)

		End Forall

	End If 

	

	If ExistingDoc Then

		Call doc.CopyAllItems(destdoc, True) 'Update existing document

		u% = u% + 1

	Else 

		Set destdoc = destDb.CreateDocument

		Call doc.CopyAllItems(destdoc, True) 'create new document

		destdoc.UNIVERSALID = doc.UNIVERSALID 

		n% = n% + 1

	End If

	

	Call destdoc.Save(True, True)

	Set doc = view.GetNextDocument( doc )

	Set destdoc = Nothing

Wend

Print "Updated " & u% & " and created " & n% & " document(s)."

End Sub

Subject: Copy, updated and delete documents from one to another db

Hi Stefanie

You can make a view in the destination database with selection formula SELECT @All. Let’s say the view name is “NewView”. Then you can add the following code at the end of your present code just before the Print statement(I haven’t tested it so please go through it throughly before executing):

=========

Dim viewNew as notesview

Dim docNew as notesdocument

Dim tempdoc as notesdocument

Dim docOther as notesdocument

Dim flag as Variant

set viewNew = destdb.getview(“NewView”)

set docNew = viewNew.GetFirstDocument

while Not docNew is Nothing

set docOther = db.GetDocumentByUnid(docNew.UniversalId)

If docOther is Nothing Then

set tempdoc = docNew

Else

set tempdoc = Nothing

End If

set docNew = viewNew.GetNextDocument(docNew)

If Not tempdoc is Nothing Then

flag = tempdoc.Remove(True)

d% = d% + 1

End If

Wend

======

Then modify the Print statement as given below:

Print "Updated " & u% & “, deleted " & d% & " and created " & n% & " document(s).”

Subject: RE: Copy, updated and delete documents from one to another db

Hi,It works; thank you for your help.

Just one little “blemish”, maybe you can help me again:

When I have one document in the source and in the destination db ; now I delete it in the source, then I get the error message " Invalid universal id" when I start the script-code again. The document in the destination db will not deleted.

When I´ve more than one document in the source db it´s no problem; the deletion works.

Stefanie

Subject: RE: Copy, updated and delete documents from one to another db

Hi Stefanie,

Try modifying the code a little and see if it works:

======

Dim viewNew as notesview

Dim docNew as notesdocument

Dim tempdoc as notesdocument

Dim docOther as notesdocument

Dim flag as Variant

set viewNew = destdb.getview(“NewView”)

set docNew = viewNew.GetFirstDocument

On Error Goto processError

while Not docNew is Nothing

set docOther = db.GetDocumentByUnid(docNew.UniversalId)

Label:

If docOther is Nothing Then

set tempdoc = docNew

Else

set tempdoc = Nothing

End If

set docNew = viewNew.GetNextDocument(docNew)

If Not tempdoc is Nothing Then

flag = tempdoc.Remove(True)

d% = d% + 1

End If

Wend

Print "Updated " & u% & “, deleted " & d% & " and created " & n% & " document(s).”

Exit Sub

processError:

set docOther = Nothing

Resume Label

=========

I hope there is never a situation where a document is deleted in the destination database. Otherwise your previous GetDocumentByUnId statement will throw the same error.

Subject: RE: Copy, updated and delete documents from one to another db

Hi,thanks for your help again. It works fine.

Stefanie