Search code examples
lotus-noteslotus-dominolotusscriptlotus-formula

Find a Lotus Notes Document based on a field value


I have a field called "JobID" that needs to be unique in the notes database. My plan is to search the database for a document's JobID and ,if it is equal to the current document's JobID, alert the user and cancel the save. I can't seem to figure out how I would do this though.


Solution

  • You could shorten your code a bit, there is no need to loop through all documents...

    db.Search is very slow, I would use a view lookup instead. You could use db.FTSearch, but if the full-text index is not up-to-date, you will not get the correct return value. So just create a hidden lookup view with the job ID as the first and only column (sorted).

    You should also declare session/db/etc in your function. Make sure you always use Option Declare...

    Function JobIdIsValid (jobId As String) As Boolean
    '*** Check if a form with this project number is already created
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim view As NotesView
    Dim col as NotesDocumentCollection
    Dim searchformula As String
    
    Set db = session.CurrentDatabase
    Set view = db.GetView("(LookupJobID)")
    Call view.Refresh()   'Optional, but useful if documents are created often
    Set col = view.GetAllDocumentsByKey(JobID)
    If col.Count > 0 Then
        JobIdIsValid = False
    Else
        JobIdIsValid = True
    End If
    End Function