Sub Queryclose(Source As Notesuidocument, Continue As Variant) ' this section of code deletes existing and creates new customer DUNs documents ' (we are doing this a the getentrykey and getdocumentbykey do not get documents in a sorted column when you have ' the document displaying multiple times for multiple values) Dim s As New NotesSession Dim doc As NotesDocument ' this document (back-end) Dim db As NotesDatabase ' current db Dim v As NotesView ' view of customer's DUNs documents to delete/create Dim e As NotesViewEntry ' current view entry of a DUNs document being tested Dim elookup(1) As String ' string list of 2 variables - 0=CR_ID, 1=DUNs Number Dim eCol As NotesViewEntryCollection ' collection of found customer DUNs documents Dim dunDoc As NotesDocument ' current DUNs document being created Dim dunsarray As Variant ' the values of the duns field Dim dunsmatch As String ' string containing DUNs number to match Dim havematch As Integer ' temporary working integer for detecting when we have found a match to a DUNs Set doc = Source.Document Set db = s.CurrentDatabase Set v = db.GetView("LUCDUNbyID") ' view of customer DUNs documents by customer id Call v.Refresh ' refresh the view in case the document was created and the view index has not yet been updated elookup(0) = doc.CR_ID(0) ' get the current duns values dunsarray = doc.GetItemValue( "CR_CustomerDuns" ) Forall d In dunsarray ' see if this DUNS number already exists elookup(1) = d Set e=v.GetEntryByKey(elookup, True) If Not (e Is Nothing) Then ' get entry document Set dunDoc = e.Document Else ' create new DUN document Set dunDoc = db.CreateDocument dunDoc.Form = "CustDUN" End If ' update values either way dunDoc.CR_ID = elookup(0) dunDoc.CR_Customer = doc.CR_Customer(0) dunDoc.CR_CustomerDuns = d dunDoc.PUNID = doc.UniversalID Call dunDoc.Save(True, False) End Forall ' now we need to go the other direction and see if we need to delete any... ' get current documents Set eCol=v.GetAllEntriesByKey(doc.CR_ID(0), True) ' loop through these entries and see if they are in the array (we are going to reuse "e" &"dunDoc" objects) Set e = eCol.GetFirstEntry While Not (e Is Nothing) ' loop through collection havematch=0 ' set/reset test variable to 0 Set dunDoc = e.Document dunsmatch = dunDoc.CR_CustomerDuns(0) ' duns number in current DUNs doc Forall d In dunsarray ' look for a match If Strcompare(d, dunsmatch, 1)=0 Then ' set test variable to found havematch=1 End If End Forall If havematch=0 Then ' delete the entry document as it was not found Call dunDoc.Remove(True) ' true means force End If ' get next entry Set e=ecol.GetNextEntry(e) Wend End Sub