Creating new documents from info in another database

I’m script illiterate so I need a little help here. I need to develop a new database with a separate document for each employee to track certain training status. Rather than create over 1500 documents from scratch, I’d like to be able to pull basic fields such as name, jobtitle, location, etc., from our Employee Directory database and have a new document created with those fields in the new database. I’d also like to occasionally run the agent again to update and append any new employees, but not duplicate any documents for those employees where nothing has changed. Any help here? Thanks in advance!

Subject: Creating new documents from info in another database

Heres some code that will get you started. It will create documents in your new database from all documents in a view in your employee directory.

Dim session as new notessession

Dim db as notesdatabase

Dim employeeedb as notesdatabase

Dim view as notesview

Dim doc as notesdocument

Dim newdoc as notesdocument

Set db = session.currentdatabase

Set employeedb = new notesdatabase(“servername”, “dbname”)

Set view = employeedb.GetView(“ViewName”)

Set doc = view.GetFirstDocument

While not doc is nothing

Set newdoc = db.CreateDocument

newdoc.Form = “FormName”

newdoc.name = doc.name(0)

newdoc.jobtitle = doc.jobtitle(0)

'set rest of fields

Call newdoc.Save(True, False)

Set doc = view.GetNextDocument(doc)

Wend

With regard to keeping these documents updated it is probably best to add some code to the onsubmit event in the form in the employee database. You could then set a flag field to say “Amendment” if the document has changed or “New” if a new document has been created. A scheduled agent could then be run from your new database that finds these documents and either amends an existing document or creates a new one.

Alternatively it could be coded so that your new database is updated immediately a document is added or amended in the Employee Directory.

Another thing that you may need to check is whether a document is deleted in the Employee directory. You could add some code to the database script section in designer that would also delete the document in your new database.

Subject: RE: Creating new documents from info in another database

Thank you both, Matt and Stephen. This is a great forum for a newbie like me.

Subject: Creating new documents from info in another database

Nothing hard about what you want to do.

Here is a starting point, I wrote this off the top of my head so some of syntax maybe wrong. It’s meant as a starting point so you can learn:

dim s as new notessession

dim thisDB as NotesDatabase

dim otherDB as NotesDatabase

dim otherDBView As NotesView

dim thisDBDoc As NotesDocument

dim otherDBDoc As NotesDocument

Set thisDB = s.currentdatabase

Set otherDB = new Notesdatabase(“servername”,“path\to\filename.nsf”)

if not otherDB is nothing then

Set otherDBView = otherDB.getview(“viewname”)

if not otherDBView is nothing then

otherDBDoc = View.GetfirstDocument

while Not otherDBDoc Is Nothing

  Set thisDBDoc = thisDB.createDocument

  thisDBDoc.Form = "TheFormName"

  thisDBDoc.FirstName = otherDBDoc.FirstName

  thisDBDoc.LastName = otherDBDoc.Surname

  ...

  Call thisDBDoc.Save(False,False)

  Set OtherDBDoc = OtherDB.getnextdocument(OtherDBDoc)

Wend

End If

End if

That will loop through the documents in the specified view and create new documents in your new Database.

If you wanted to copy all the field values in the document - there is a copy all method you can use.

That will get you started. As for the second part, you have two options

(1) You could mark the documents in the “otherDB” to indicate they have been copied and add to the loop above if the flag is set skip that document.

(2) Assuming you don’t want to add anything to the other DB, for each document as you get to it, check to see if it already exist in this DB. If so skip it and go to the next document. For your learning check out the method search under database or getalldocumentsbykey on a view.

Good learning.