Search code examples
lotus-domino

How to check document already exist in IBM Domino


I'm working on create an agent to move cancelled documents to archive database, before copy to archive database, I want to check if the document is already existed in archive database. There are some documents' main fields are same in database, so I can't use this fields to check if it is the same one. Are there any way to check if the document is the same document in two databases? I found that for same document, some part of unid are same in two databases(for example: unid in source database: 613D530A7B107F46852578E9001DCC89 unid in dest database: 85258289002735FB852578E9001DCC89), but I'm not sure if this is a correct flag.


Solution

  • As the unid of documents (if not tampered with) consists of a a part computed from the database replica ID and a converted timestamp for "created" it is not pure coincidence, that the "same" document has a similar unid.

    But this is NOTHING you can rely on and depends on the way you create your documents in the archive.

    If you did something like

    Set docArchive = New NotesDocument( dbArchive )
    Call doc.CopyAllItems( docArchive, True )
    

    Then the unids would not have anything to do with each other.

    If you use doc.CopyToDatabase, it will depend on the number of attempts and may result in

    • same unid in target
    • similar unid in target (first copy of document)
    • totally different unid in target (subsequent copies)

    To identify your document you have to have a "key" for finding it.

    One way would be to use the SAME universalid:

    Set docArchive = New NotesDocument( dbArchive )
    Call doc.CopyAllItems( docArchive, True )
    docArchive.Universalid = doc.Universalid
    Call docArchive.Save()
    

    Then you could check for existance like:

    On Error Resume Next
    Set docArchive = dbArchive.getDocumentByUnid( doc.UniversalID )
    On error Goto 0
    If Not docArchive is Nothing then 'EXISTS
        ....
    End If
    

    If you don't want to go with the universalid directly, you could compute a key or again use the universalid of the source document as key:

    Set docArchive = doc.CopyToDatabase( dbArchive )
    strArchiveKey = doc.Universalid
    'or compose unique key from 3 individual fields:
    strArchiveKey = doc.getItemvalue( "OneField" )(0) & "-" & doc.getItemvalue( "AnotherField" )(0) & "_" doc.getItemvalue( "YetAnotherField" )(0)
    Call docArchive.ReplaceitemValue( "ArchiveKey", strArchiveKey  )
    Call docArchive.Save(True, True, True)
    

    And then find the archive document from a search or better from a GetDocumentByKey in a view sorted by ArchiveKey:

    Set docArchive = db.Search( {ArchiveKey = "} & strArchiveKey & {"}, Nothing, 0).getFirstDocument()
    
    Set docArchive = viwLkp.GetDocumentByKey( strArchiveKey )