Merge two docs to a newly created doc

I have a database that when a person clicks a button two documents are created. One person fills in one form and another person fills in the other. At some point I need to create a new document and merge the fields from the two separate docs to the newly created one. I have this code. The mergeview is cat. by UNID of a single document:Sub Initialize

Dim db As NotesDatabase	

Dim mergeview As NotesView	

Dim doc As notesdocument

Dim doc2 As notesdocument

Dim doc3 As notesdocument

Dim doc4 As notesdocument

Dim formval As String

Dim collection As notesdocumentcollection

Dim empdoc As notesdocument

Dim forms As String

Dim val1 As String

Dim val2 As String

Dim key As String

Dim newdoc As notesdocument

Dim empdocid As Variant

Set db = New NotesDatabase( "", "Test\perfguide.nsf" )

Set mergeview = db.getview("mergeview")

Set doc = mergeview.GetFirstDocument



While Not (doc Is Nothing)

	

	If doc.form(0) = "Employee Form" Then

		key = doc.GetItemValue("thisdocid")(0)

	Else

		If doc.form(0) = "Managers Form" Then

			key = doc.GetItemValue("thisdocid")(0)

		Else

			If doc.form(0) = "Mgr Employee Form" Then

				key = doc.GetItemValue("thisdocid")(0) 

			Else

				If doc.form(0) = "Mgr Managers Form" Then

					key = doc.GetItemValue("empdocid")(0)

				End If

			End If

		End If

	End If

	Set collection = mergeview.GetAllDocumentsByKey(key, False)

	If collection.Count = 2 Then

		Set doc2 = collection.GetFirstDocument

		Set doc3 = collection.GetNextDocument(doc2)

		val1 = doc2.getitemvalue("Form")(0)

		val2 = doc3.getitemvalue("Form")(0)

		forms = val1 + " " + val2

	End If

	If forms = "Employee Form Mgr Employee Form" Then

		Set doc4 = New NotesDocument( db )

		doc4.form = "Employee Merged Form"

		formval = "Employee Merged Form"

		Call doc4.Save(True,True)

	Else

		If forms = "Mgr Employee Form Employee Form" Then

			Set doc4 = New NotesDocument( db )

			doc4.form = "Employee Merged Form"

			formval = "Employee Merged Form"

			Call doc4.Save(True,True)

		Else

			If forms = "Mgr Managers Form Managers Form" Then

				Set doc4 = New NotesDocument( db )

				doc4.form = "Managers Merged Form"

				formval = "Managers Merged Form"

				Call doc4.Save(True,True)

			Else

				If forms = "Managers Form Mgr Managers Form" Then

					Set doc4 = New NotesDocument( db )

					doc4.form = "Managers Merged Form"

					formval = "Managers Merged Form"

					Call doc4.Save(True,True)

				End If

			End If

		End If

	End If

	Call doc2.CopyAllItems( doc4 , True )

	Call doc4.Save(False,False)

	Call doc3.CopyAllItems(doc4 , True)

	doc4.form = formval

	

	doc2.merged = "Merged"

	success = doc2.ComputeWithForm( False, False )

	If success Then

		Call doc2.Save( True, True )

	End If

	

	doc3.merged = "Merged"

	success = doc2.ComputeWithForm( False, False )

	If success Then

		Call doc2.Save( True, True )

	End If

	

	

	Call doc4.Save(True,True)

	Call mergeview.Refresh

	Set doc = mergeview.GetfirstDocument

wend

This does not seem to be working the way I would like it too. I can not see what I am doing wrong. Can I have some more eyes take a look. Been looking at it far too long.

Any suggestions…

TIA,

Steve

Subject: Merge two docs to a newly created doc

Steve, Try

Set doc = mergeview.GetNextDocument(doc) at the end instead of Set doc = mergeview.GetfirstDocument

Subject: RE: Merge two docs to a newly created doc

Deborah,

I did have that but it kept telling me that the doc was not in the view.

Not sure what that means.

Steve

Subject: Merge two docs to a newly created doc

Steve

You say it’s not working how you’d like it to work - how is it working and how would you like it to work? What’s it actually doing at the moment - giving an error or just not merging the documents?

What does the mergeview look like? Does it actually have the two documents to be merged under a single category? What does the thisdocid field hold (and the empdocid field - why are these fieldnames different)? If it does actually hold the UNID of each document then I’d guess that the view wouldn’t categorise properly, therefore you wouldn’t find the two documents to merge.

Dan

Subject: RE: Merge two docs to a newly created doc

Dan,

The column formula looks like this:

@If(Form = “Employee Form” ; ThisdocID ; Form = “Managers Form” ; thisdocid ; Form = “Mgr Employee Form” ; Empdocid ; form = “Mgr Managers Form” ; Empdocid ; “”)

It is displaying correctly.

The selection formula is

SELECT (Form = “Employee Form”) | (Form = “Managers Form”) | (Form = “Mgr Employee Form”) | (Form = “Mgr Managers Form”) & merged != “Merged”

When I run the code it only acts upon the first document in the view repeatedly. It does merge the doc but then it keeps running on the same doc.

Steve

Subject: RE: Merge two docs to a newly created doc

Not sure straight away, but in your code the Mgr Employee form gives a key using thisdocid, whereas the column formula uses empdocid for that form.

I’d also change the view selection formula to:

SELECT Form = “Employee Form”:“Managers Form”:“Mgr Employee Form”:“Mgr Managers Form” & merged!=“Merged”

Your current selection formula could give problems as the form statements aren’t linked separately to the merged field check.

Also, just in the interest of cleaning code up, you use a lot of duplication in your If statements. For example, the column formula could easliy just be:

@if(Form=“Employee Form”:“Managers Form”;thisdocid;Empdocid)

The same sort of thing goes for your lotusscript code.

Let me know if any of this helps, I’ll have a further look though

Dan

Subject: RE: Merge two docs to a newly created doc

More clearification.

Supervisor goes into database. clicks on an employee profile doc. Button to create documents. After supervisor and employee fill out these documents the two documents need to be merged into one.

The new document has the same field names as the two docs.

The button to create the two docs looks like so:

Sub Click(Source As Button)

Dim workspace As New NotesUIWorkspace

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim doc2 As notesdocument

Dim Lookupdoc As notesdocument

Dim uidoc As Notesuidocument

Dim DocName As Variant

Dim View As notesview

Dim id As String

Dim thisdocid As String

Dim test As String

Dim memo As notesDocument



Dim SupLookup As String



Dim collection As notesdocumentcollection

Dim searchfor As String



Dim bodyItem As notesRichtextItem

Set db = session.CurrentDatabase

'Set View = db.GetView( "My Report1" )

Set uidoc = workspace.CurrentDocument   

Call uidoc.save

Set view = db.GetView( "(SupLookup)" )

'create employee doc and populate pertinent fields



Set doc = db.CreateDocument

key = Cstr(uidoc.FieldGetText("NotesName"))

Set Lookupdoc = view.GetDocumentByKey (key )

If  (Lookupdoc Is Nothing) Then	

	doc.Form = "Employee Form"

	doc.Responsible = Cstr(uidoc.FieldGetText("NotesName"))

Else

	doc.form = "Managers Form"

	doc.Responsible = Cstr(uidoc.FieldGetText("Supervisor"))

End If

DocName = doc.form

doc.EmployeeName = Cstr(uidoc.FieldGetText("NotesName"))

doc.Department = Cstr(uidoc.fieldgettext("departcode"))

doc.PositionTitle = Cstr(uidoc.fieldgettext("jobname"))

doc.Supervisor=Cstr(uidoc.fieldgettext("Supervisor"))

doc.EmployeeClock=Cstr(uidoc.fieldgettext("EmpCode"))

doc.JobCode = Cstr(uidoc.fieldgettext("JobCode"))

doc.Responsible = Cstr(uidoc.FieldGetText("NotesName"))

doc.IDdoc = Cstr(uidoc.Document.universalid)

Call doc.Save(False,False)

On Error Goto errHandle



Dim AnewValues( 1 To 2 ) As String

AnewValues( 1 ) = uidoc.fieldgettext("NotesName")

AnewValues( 2 ) = ""

Dim authorsItem As New NotesItem(doc, "Authors",  _

AnewValues, AUTHORS)



Dim RnewValues( 1 To 3 ) As String

RnewValues( 1 ) = uidoc.fieldgettext("NotesName")

RnewValues( 2 ) = ""

'RnewValues( 3 ) = uidoc.fieldgettext("Supervisor")

RnewValues( 3 ) = ""

Dim readersItem As New NotesItem(doc, "Readers",  _

RnewValues, READERS)



doc.mgrdoc = uidoc.document.universalid



'----mail doclink to the employee

’ Set memo = session.currentDatabase.createDocument

’ Set bodyItem = memo.createRichtextItem(“Body”)

’ memo.Form = “Memo”

’ memo.Principal = uidoc.FieldGetText(“Supervisor”)

’ memo.Subject = “Performance Review Created”

’ bodyItem.appendText(“Your Supervisor has initiated the Performance Review process. Please follow the doclink to begin filling out your review.”)

’ Call bodyitem.AppendDocLink ( doc, “DocLink to Performance Guide” )

’ Call memo.Save(True,True )

’ Call memo.send(False, uidoc.FieldGetText(“NotesName”))

'save and security

Call doc.Save( False, False )

id = doc.UniversalID

'Call uidoc.FieldSetText( "EmpID", id )

Call uidoc.FieldSetText("notificationsendt","Yes")

Call uidoc.FieldSetText("Status","Performance Guide Created")

'Call uidoc.FieldSetText("Authors", uidoc.fieldgettext("Supervisor") + ", ")

'Call uidoc.FieldSetText("Readers", uidoc.fieldgettext("Supervisor") + ", ")

'Call uidoc.fieldsettext("SaveOptions", "1")

Call uidoc.fieldsettext("Status", "Employee Review Created")

Call uidoc.save

’ Call uidoc.fieldsettext(“SaveOptions”, “0”)

Call uidoc.refresh

Call uidoc.save







Msgbox "Employee's Review Successfully Created"



'create manager doc and populate pertinent fields



Set doc2 = db.CreateDocument

If (doc.Form(0) ="Employee Form") Then

	doc2.Form = "Mgr Employee Form"

Else

	If (doc.Form(0) ="Managers Form") Then

		doc2.Form = "Mgr Managers Form"

	End If

End If

doc2.Responsible = Cstr(uidoc.FieldGetText("Supervisor"))

doc2.EmployeeName = Cstr(uidoc.FieldGetText("NotesName"))

doc2.Department = Cstr(uidoc.fieldgettext("departcode"))

doc2.PositionTitle = Cstr(uidoc.fieldgettext("jobname"))

doc2.Supervisor=Cstr(uidoc.fieldgettext("Supervisor"))

doc2.EmployeeClock=Cstr(uidoc.fieldgettext("EmpCode"))

doc2.JobCode = Cstr(uidoc.fieldgettext("JobCode"))

doc2.Empdocid = doc.UniversalID



Dim AnewValues1( 1 To 2 ) As String

AnewValues1( 1 ) = uidoc.fieldgettext("Supervisor")

AnewValues1( 2 ) = ""

Dim authorsItem2 As New NotesItem(doc2, "Authors",  _

AnewValues1, AUTHORS) 





Dim RnewValues1( 1 To 3 ) As String

RnewValues1( 1 ) = uidoc.fieldgettext("Supervisor")

RnewValues1( 2 ) = ""

RnewValues1( 3 ) = ""

Dim readersItem2 As New NotesItem(doc2, "Readers",  _ 

RnewValues1, READERS)

Call doc2.save( False, False )

id = doc2.UniversalID

Call uidoc.FieldSetText( "MgrID", id )

Call doc2.save( False, False )

doc.mgrdocid = doc2.UniversalID

success = doc.ComputeWithForm( False, False )

If success Then

	Call doc.Save( True, True )

End If



Goto leave

Call uidoc.close