Navigation
User login
Recent blog posts
- AweSync is launched in production!
- We Moved to a New Office
- Lotus Notes ActiveX Grid announced - FREE Trial
- Lotus Traveler - Nice Way To Sync Mobile Device
- Creating a Lotus Notes View with NotesView and AJAX
- How to Fix a Multiline Comment in a Lotus Notes Application
- Filtering in Views Using LotusScript Code Made Easier
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
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
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
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
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
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
, 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



- Thank you
- Thank you