Generic Function To Check Duplicate Documents Based on Key Field

You Can View User Feedback To This Tip

This is a generic function to check for any duplicate documents based on a lookup view and lookup key column. It will handle both new and saved document checking. The check on saved document is necessary as the key in the document may be editable after it is saved.


Function IsDocumentDuplicate (db As NotesDatabase, viewName As String, _
keyColumnName As String, doc As NotesDocument, errorMsg As String)
	
	IsDocumentDuplicate = False
	
	Dim viewLookup As NotesView
	Dim docLookup As NotesDocument
	Dim errorString As String
	
	errorString = "Document already exists in the database!"
	Set viewLookup = db.GetView(viewName)
	If Not viewLookup Is Nothing Then
		Set docLookup = viewLookup.GetDocumentByKey(keyColumnName, True)
		If Not docLookup Is Nothing Then
			If doc.IsNewNote Then
				errorMsg = errorString
				Exit Function
			Else
				If doc.UniversalID <> docLookup.UniversalID Then
					errorMsg = errorString
					Exit Function
				End If
			End If
		Else
			errorMsg = "Error getting doc object for lookup document!"
			Exit Function
		End If
	Else
		errorMsg = "Error getting view object for lookup document!"
		Exit Function
	End If
	
	IsDocumentDuplicate = True
	
End Function

USER FEEDBACK TO THIS TIP

  • This tip is missing some rather interesting details:
    - If you are testing from a new document (not saved) it will return False if no other documents are having the same key AND
    - If you are testing from the first document created with the key you'll get the True return-value. (By the way: when then name of the function is IsDocumentDuplicate the false-value should be True!!)

    Here is what I would have done it as:

    Function IsDocumentDuplicate (db As NotesDatabase, viewName As String, _ keyColumnName As String, doc As NotesDocument, errorMsg As String)

    IsDocumentDuplicate = True
    
    Dim viewLookup As NotesView
    Dim docLookupcol As NotesDocumentCollection
    Dim docLookup As NotesDocument
    dim DuplicateCount as long
    
    Set viewLookup = db.GetView(viewName)
    if not viewLookup is nothing then
      Set docLookupcol = viewLookup.GetAllDocumentsByKey(keyColumnName, True)
      if docLookupCol.Count then
        If doc.IsNewNote then
          errorMsg = "A document allready exists with the key: " & keyColumnName
          Exit Function
        else
          DuplicateCount = 0
          Forall doclookup in doclookupcol
            If doc.UniversalID = docLookup.UniversalID Then
              DuplicateCount = DuplicateCount + 1
            end if
          end forall
          If DuplicateCount > 1 then
            errorMsg = "Error: Duplicate documents with the key: " & keyColumnName
            Exit Function
          end if
        end if
    
      else
        If doc.IsNewNote then
          IsDocumentDuplicate = False
          Exit Function
        else
          errorMsg = "Error getting docs object for lookup documents! This 
    document has been deleted!!"
          Exit Function
        end if
      end if
    Else
      errorMsg = "Error getting view object for lookup document!"
      Exit Function
    End If
    
    IsDocumentDuplicate = False
    
    end function
    —Jes Ronving

Dig Deeper on Domino Resources

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close