Reply to comment

Filtering in Views Using LotusScript Code Made Easier

Filtering documents within a view in LotusScript straightforwardly can require quite a lot lines of code with various functions. However, by simple modular programming, you can use less functions in the code, making code more friendly and readable, thus reducing the chances to make a mistake, and increasing re-usability.

Here's the sample of straightforward code:

dim view as NotesView
set view = db.GetView("myView")
Dim doc As NotesDocument
call view.Refresh()
dim name as string
dim evaluation
evaluation = evaluate("@username")
name = evaluation(0)
Set doc = view .GetFirstDocument()
Do While Not doc Is Nothing    
        if doc.user(0) = name then
                ' some operations
        end if
        Set doc = view .GetNextDocument(doc)
loop

Here is the code, when using modular approach:

dim collection as NotesDocumentCollection
Dim doc As NotesDocument
set collection = FilterView(GetDbView(db, "MyView", True), {user = @UserName})
Set doc = collection .GetFirstDocument()
Do While Not doc Is Nothing    
        ' some operations
        Set doc = collection .GetNextDocument(doc)
loop

And here are the functions, which make working with views much easier:

Private AllViewsList List As NotesView

Function FilterView(view As NotesView, filterFormula As String) As NotesDocumentCollection
        On Error Goto ErrorHandler

        Dim collection As NotesDocumentCollection
        Dim doc As NotesDocument, docTemp As NotesDocument

        Set collection = view.GetAllDocumentsByKey("")
        While collection.Count <> 0
                Call collection.DeleteDocument(collection.GetLastDocument())
        Wend

        Set doc = view .GetFirstDocument()
        Do While Not doc Is Nothing
                Set docTemp = view .GetNextDocument(doc)
                If Cbool(EvaluateFormula(filterFormula, Doc, False)(0)) Then
                        Call collection.AddDocument(doc)
                End If
                Set doc = docTemp
        Loop

        Set FilterView = collection
        Exit Function
ErrorHandler:
        Error Err, Lsi_info(3) + "/" + Lsi_info(2) + "(" + Cstr(Erl) + ")" +  ":" + Chr(13) + Error$
End Function

Function EvaluateFormula(Formula As String, TargetDoc As NotesDocument, doCheckSyntax As Boolean)
        On Error Goto ErrorHandler
        Dim Result

        If doCheckSyntax Then
                Call CheckFormulaSyntax(Formula, True)
        End If

        If targetDoc Is Nothing Then
                Result = Evaluate (Formula)
        Else
                Result = Evaluate (Formula, TargetDoc)
        End If
        EvaluateFormula = Result

        Exit Function
ErrorHandler:
        Error Err, Lsi_info(3) + "/" + Lsi_info(2) + "(" + Cstr(Erl) + ")" +  ":" + Chr(13) + Error$
End Function

Function CheckFormulaSyntax(Formula As String, doRaiseException As Boolean) As Variant
        On Error Goto ErrorHandler

        Dim result
        result = Evaluate ("@CheckFormulaSyntax({"+Replace(Formula, "\", "\\")+"})")
        If (result(0) <> "1") And doRaiseException Then
                Error 1, "Error in formula syntax: " + result(0) + " at line " + result(1)
        End If

        CheckFormulaSyntax = result

        Exit Function
ErrorHandler:
        Error Err, Lsi_info(3) + "/" + Lsi_info(2) + "(" + Cstr(Erl) + ")" +  ":" + Chr(13) + Error$
End Function

Function GetDBView (db As NotesDatabase, Byval ViewName As String _
, NeedRefreshFirst As Integer, NeedRefresh As Integer) As NotesView
        On Error Goto ErrorHandler
        ' Getting View by View Name

        If db Is Nothing Then Exit Function
        If ViewName = "" Then Exit Function

        If Not Iselement (AllViewsList (db.ReplicaID + "~~~" + ViewName)) Then
                Set GetDBView = db.GetView (ViewName)
                If GetDBView Is Nothing Then Exit Function
                Call GetDBView.Refresh
                Set AllViewsList (db.ReplicaID + "~~~" + ViewName) = GetDBView2
        Else
                Set GetDBView = AllViewsList (db.ReplicaID + "~~~" + ViewName)
                If NeedRefresh Then Call GetDBView2.Refresh
        End If
        Exit Function
ErrorHandler:
        Error Err, lsi_info(3) + "/" + lsi_info(2) + "(" + Cstr(Erl) + ")" +  ":" + Chr(13) + Error$
End Function


Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.

Poll

Which realtime collaborative platforms are you using mostly in your business?: