I have a database with a number of master documents. When a new acquisition is added, I would like to make a copy of these documents, but change the acquisition field to reflect the name of the new company.
Here is my code:
'sFieldName = Universal ID
Set doc = db.GetDocumentByUNID(sFieldName)
If Not (doc Is Nothing) Then
Set doc2 = New NotesDocument(db)
doc2.Form = "AddActionItem_new"
Call doc.CopyAllItems(doc2, True)
doc2.Acquisition = "LISA TEST"
Call doc2.Save( True, True )
End If
I had to set doc2.Form or I would get a ‘Cannot locate default form’ error when trying to access the newly created document.
The new document is being created, and the acquisition field is being updated to reflect the new name, but the rest of the data stored in fields in the master document are not being carried over.
I want an exact duplicate - only the acquistion field should be different.
Any ideas or suggestions would be greatly appreciated - thanks in advance.
Subject: Using CopyAllItems to create duplicate Document - Code included
Lisa, you can copy field by field:
Dim doc As notesdocument
Dim uidoc As notesuidocument
Dim ws As NotesUIWorkspace
Dim db As NotesDatabase
Dim session As New NotesSession
Dim NewDoc As NotesDocument
Set db = session.currentdatabase
Set ws = New NotesUIWorkspace
Set uidoc = ws.Currentdocument
Set doc = uidoc.document
Set NewDoc = New NotesDocument(db)
NewDoc.Form = "FormName"
NewDoc.field1 = doc.Field1(0)
NewDoc.field2 = doc.Field2(0)
NewDoc.field3 = doc.Field3(0)
Call newdoc.Save(True, False)
HTH
ST
Subject: RE: Using CopyAllItems to create duplicate Document - Code included
Yes but what happens if you don’t know all the fields of that document.
Dim s As New NotesSession
Dim currentdb As NotesDatabase
Dim doc As NotesDocument
Dim newdoc As NotesDocument
Set currentdb = s.CurrentDatabase
’ assuming you have assigned doc
Set newdoc = currentdb.Createdocument
Forall item In doc.Items
newdoc.Copyitem(item, "")
End Forall
newdoc.Acquisition = “LISA TEST”
Call newdoc.Save(True, False)
Also you could try.
Dim s As New NotesSession
Dim currentdb As NotesDatabase
Dim doc As NotesDocument
Dim newdoc As NotesDocument
’ assuming you have assigned doc
Set currentdb = s.CurrentDatabase
Set newdoc = doc.CopyToDatabase(currentdb)
newdoc.Acquisition = “LISA TEST”
Call newdoc.Save(True, False)
Subject: RE: Using CopyAllItems to create duplicate Document - Code included
I’ve tried each of the methods that you have suggested, both of which are giving me the same result - the new document is created with the acquisition field set, but all other fields on the form are empty.
I have some rich text fields on the form - could this be causing a problem?
Thanks for the help - any other ideas would be greatly appreciated.
Subject: RE: Using CopyAllItems to create duplicate Document - Code included
You have the right approach. Ignore those people who are telling you to copy field by field. Rich text will not be a problem, and even if it were, it would not prevent you copying other fields. The CopyToDatabase that Martin suggests will also work, though it is not as efficient since it saves the document twice.
If you use CopyAllItems, set doc2.Form after you copy all the items. Otherwise you end up with two Form items, one with a wrong value.
I don’t know why you would not be getting all the document data the way you’re doing it. How do you know that this data is not being saved? Since you’ve apparently tried four different ways and don’t see the field data using any of them, I suspect there’s some problem other than what you think it is. For instance, the data is not there in the source document in the first place, or you’re copying from the wrong document, or the destination form doesn’t display the fields that are there.
Have you used the debugger to find out whether the fields are in fact present in the original document? Or that you really are finding the original document? That the process that you’re using to come up with a UNID, comes up with the right UNID? Why are you using a UNID in any case? If you’re copying a set of master documents, why aren’t you fetching them from a view, so that you just iterate thru the view using GetFirstDocument/GetNextDocument?
Apparently your copy is happening from a database into that same database. And yet, if you open the new document without having assigned the Form field, you’re told that there’s no form to display the document. To use the words of a famous police detective, I find that “highly suggestible.” What documents are these you’re copying that haven’t already got a form to display them with?