Problem with UNID using MakeResponse

I have a view, the user selects a document(s) in a view and then clicks a button to create a schedule A invoice (response document) The problem is that every once and a while the $ref and parentID value that are set in the response doc are not correct. When this happens both the $ref & parentID will be the same but they will not be the UNID of any document in the entire database. If we search the entire db for the UNID the orphaned doc was given it does not exist. (I set up a special full text view with every doc every created of every type just to be sure) The only record that will show up in the full text search is the orphaned response doc with that value in the parentID field. What could be intermittenly causing the MakeResponse and the parentID to be set wrong to some phantom UNID? I cannot duplicate the problem no matter how much testing I do. It works fine for me every time on my machine. Could it be a local client problem, there are reports of this one two machines and is intermittent on those machines? It seems to have flared up here in the last 30 days and we haven’t done anything to this agent/code at all. Though today it worked fine. I can post some sample UNIDs if that would help?

Here is the code:

Sub Initialize

Dim session As New NotesSession 

Dim db As notesdatabase 

Dim dc As notesdocumentcollection 

Dim doc As notesdocument, doc2 As notesdocument, doc3 As notesdocument 

Dim newdoc As NotesDocument 

Dim DupLookupView As NotesView

Dim DupDoc As notesdocument

Dim DupDocId As String

Dim a1 () As String 

Dim a2 () As String 

Dim a3 () As String 

Dim a4 () As String 

Dim a5 () As String 

Dim a6 () As String 

Dim a7 () As String 

Dim a8 () As String

Dim a9 () As String

Dim a10 () As String

Dim a11 () As String

Dim a12 () As String

Dim a13 () As String

Dim a14 () As String

Dim a15 () As String

Dim a16 () As String



Set db = session.currentdatabase 

Set dc = db.unprocesseddocuments 

Set doc = dc.getfirstdocument 



If doc.Other(0) ="" Then

	Other = "0"

End If



While Not(doc Is Nothing) 

	'If doc.docPrefix(0) = "F"  Then

		'Messagebox "Your selection box highlighted an in house lease but you checked interest rate worksheets in the gutter.  Reselect the worksheets while making certain the selection box is highlighting an interest rate based deal."

		'Exit Sub

	'End If

	

	

	Set newdoc = db.CreateDocument 

	newdoc.Form = "Schedule A Invoice" 

	Call newdoc.MakeResponse(doc)		

	newdoc.parentid = doc.universalid	

	newdoc.InHouseBank = doc.InHouseBank(0)

	newdoc.CusName = doc.CusName(0)

	'newdoc.CusStreetAddress = doc.CusStreetAddress(0)

	'newdoc.CusStreetAddress2 = doc.CusStreetAddress2(0) 

	'newdoc.CusSuite = doc.CusSuite(0) 

	'newdoc.CusCity = doc.CusCity(0)

	'newdoc.CusState = doc.CusState(0)

	'newdoc.CusZip = doc.CusZip(0) 		

	newdoc.SalesMan = doc.SalesMan(0) 

	newdoc.Other = Other

	IHTax = doc.IHTax(0)

	IHToLeasePay = doc.IHToLeasePay(0)

	IHTotal = doc.IHTotal(0)

	IRInterimTotal = doc.IRInterimTotal(0) 	

	IRInterimTax = doc.IRInterimTax(0)

	newdoc.IHTotal = Cstr(IHTotal)

	newdoc.IHCapRed = Cstr(IHCapRed)

	newdoc.IHCapRedTax = Cstr(IHCapRedTax)

	

	newdoc.IRInterimTotal = Cstr(IRInterimTotal) 		

	newdoc.IRInterimTax = Cstr(IRInterimTax) 

	newdoc.IHTax = Cstr(IHTax)

	newdoc.IHToLeasePay = Cstr(IHToLeasePay)

	Set Doc2 = Doc 

	I = 0 

	While Not(Doc2 Is Nothing) 

		

		'If doc.InHouseBank(0) <> doc2.InHouseBank(0) Then

		'	Messagebox "The banks are not the same for all worksheets.  The invoice will not be created.  Reselect worksheets with the same bank."

		'	Exit Sub

		'End If

		

		'Lookup against active view to check for duplicate schedule A

		DupDocId = doc2.DocNumConvert(0)

		Set DupLookupView = db.GetView("(LookupScheduleAInvoice)")

		Set DupDoc = DupLookupView.GetDocumentByKey(DupDocId)

		If DupDoc Is Nothing Then 

			'Test

		Else

			Messagebox "Lease #: " + DupDocId + " has already been used on a Schedule A invoice.  Either mark the existing Schedule A invoice inactive or repeat this process and do not select lease " +DupDocId	+" ."

			Exit Sub

		End If

		

		If Doc.CusName(0) = Doc2.CusName(0)  Then 

			Redim Preserve a1 ( 0 To I ) As String 

			Redim Preserve a2 ( 0 To I ) As String 

			Redim Preserve a3 ( 0 To I ) As String 

			Redim Preserve a4 ( 0 To I ) As String 

			Redim Preserve a5 ( 0 To I ) As String 

			Redim Preserve a6 ( 0 To I ) As String 

			Redim Preserve a7 ( 0 To I ) As String 

			Redim Preserve a8 ( 0 To I ) As String 

			Redim Preserve a9 ( 0 To I ) As String

			Redim Preserve a10 ( 0 To I ) As String

			Redim Preserve a11 ( 0 To I ) As String

			Redim Preserve a12 ( 0 To I ) As String

			Redim Preserve a13 ( 0 To I ) As String

			Redim Preserve a14 ( 0 To I ) As String

			Redim Preserve a15 ( 0 To I ) As String

			Redim Preserve a16 ( 0 To I ) As String

			

			

			

			a1(i) = doc2.DocNumConvert(0) 

			a2(i) = doc2.AutoYear(0) 

			a3(i) = doc2.AutoMake(0) 

			a4(i) = doc2.AutoModel(0) 

			a5(i) = doc2.AutoSxN(0)

			a6(i) = doc2.ContractDate(0)

			a7(i) = doc2.DriName(0) 

			a8(i) = doc2.Branch(0) 

			a9(i) = Cstr(doc2.IRInterimTotal(0)) 

			a10(i) = Cstr(doc2.IRInterimTax(0))

			a11(i) = Other

			a12(i) = Cstr(doc2.IHTotal(0)) 

			a13(i) = Cstr(doc2.IHTax(0)) 

			a14(i) = Cstr(doc2.IHToLeasePay(0)) 

			a15(i) = Cstr(doc2.IHCapRed(0)) 

			a16(i) = Cstr(doc2.IHCapRedTax(0)) 

			

			I = I +1 

			Set doc3 = dc.getnextdocument(Doc2)                         

			Call dc.deletedocument(doc2) 

			Set doc2 = doc3 

		Else 

			Set Doc2 = dc.getnextdocument(Doc2)                         

		End If 

	Wend 

	newdoc.DocNumConvert = a1 

	newdoc.AutoYear = a2 

	newdoc.AutoMake = a3 

	newdoc.AutoModel = a4 

	newdoc.AutoSxN = a5 

	newdoc.CommencementDate = a6 

	newdoc.DriName = a7 

	newdoc.Branch = a8 

	newdoc.IRInterimTotal = a9

	newdoc.IRInterimTax = a10

	newdoc.Other = a11

	newdoc.IHTotal = a12

	newdoc.IHTax = a13

	newdoc.IHToLeasePay = a14

	newdoc.IHCapRed = a15

	newdoc.IHCapRedTax = a16

	

	Call newdoc.save(True,False) 

	Set doc = dc.getfirstdocument 

Wend 

End Sub