Friday, 17 June 2016

Quick Tip: GetDocumentByUnid Method in LotusScript Agents



In the past period we were frequently faced with the 4091 error message, 'Invalid universal id'  using the LotusScript GetDocumentByUNID method in an Agent.
In the specific Notes Database Application some Query definitions were used where the DocumentUnid was stored in an environment variable. The Unid was then used in an Agent to display the results of the Query definition in a Folder. Problem we frequently encountered was the error 4091 Message: Invalid universal id”
The method getDocumentByUNID NotesDatabase class returns the appropriate document for the UniversalUnid passed as argument, but it does not return Nothing if it does not find the document but the error 4091. Also it may return an irrelevant "ghost" Lotus Notes document, also known as a deletion stub.
By a colleague we were pointed to the following LotusScript function, DatabaseGetDocumentByUnid. We added this function in a Script Library and adjusted the corresponding Agent accordingly. The result: no more 4091 errors!

Function DatabaseGetDocumentByUnid (db As NotesDatabase, unid$) As NotesDocument
On Error 4091 Resume Next
Set DatabaseGetDocumentByUnid = Nothing
Set DatabaseGetDocumentByUnid = db.GetDocumentByUnid(unid$)
If Err = 4091 And ( DatabaseGetDocumentByUnid Is Nothing ) Then
Err = 0
Set DatabaseGetDocumentByUnid = Nothing
Exit Function
End If
If DatabaseGetDocumentByUnid Is Nothing Then
Set DatabaseGetDocumentByUnid = Nothing
Exit Function
End If
If DatabaseGetDocumentByUnid.Size = 0 Or DatabaseGetDocumentByUnid.UniversalID = "" Or DatabaseGetDocumentByUnid.IsDeleted Then
Set DatabaseGetDocumentByUnid = Nothing
Exit Function
End If
End Function

More information about How to correctly use the GetDocumentByUnid method in LotusScript: TechTarget - SearchDomino

1 comment:

  1. Shorter:
    Function DatabaseGetDocumentByUnid(db As NotesDatabase, unid$) As NotesDocument
    On Error Resume Next
    Set DatabaseGetDocumentByUnid = Nothing
    Set DatabaseGetDocumentByUnid = db.GetDocumentByUnid(unid$)
    Err = 0
    If DatabaseGetDocumentByUnid Is Nothing Then Exit Function
    If DatabaseGetDocumentByUnid.Size = 0 Or DatabaseGetDocumentByUnid.IsDeleted Or Not DatabaseGetDocumentByUnid.IsValid Then
    Set DatabaseGetDocumentByUnid = Nothing
    End If
    End Function

    ReplyDelete