Manage Learn to apply best practices and optimize your operations.

Use db search for a document collection

Use db search instead of looping through view for a document collection

Having maintained and supported numerous clients, I never fail to notice that most developers like to create a view filtering some documents only for another script fragment to loop through them for processing. This is rather dangerous as the code might break if the view is corrupted. If there is a need to grab a document collection for processing, we should use database level search instead. And this can be easily accomplished by using the "search" method in the NotesDatabase class.

E.g. You would like to write an agent to download sales order for China daily.

(1) The typical approach
One of the common approaches frequently used is to create a view filtering sales order documents from China.

In the agent code fragment, typically it is as follow:-

Dim session As New NotesSession
Dim db As NotesDatabase	
Dim view As NotesView
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set view = db.GetView("vChinaSalesOrder")	
Set doc = view.GetFirstDocument
Do Until doc Is Nothing		
   '... downloading processing ...
   Set doc = view.GetNextDocument(doc)

(2) The better approach
To avoid the code breaking when the view becomes corrupted, use the database level search instead. For this, you need to build a search formula first to get a document collection. Then loop through the document collection to process them.

Dim session As New NotesSession
Dim db As NotesDatabase	
Set db = session.CurrentDatabase
Dim searchFormula As String
searchFormula="Form=""frmSalesOrder"" & Country = ""China"""

Dim dc As NotesDocumentCollection
Dim doc As NotesDocument	
Set dc = db.Search(searchFormula, Nothing, 0)
If dc.Count <> 0 Then
   For i=1 To dc.Count
      Set doc = dc.GetNthDocument(i)
      '... downloading processing ...
End If

For this code fragment, there is no view involved. Hence there is no need to worry that your code might break if any views are corrupted.

Dig Deeper on Lotus Notes Domino Administration Tools

  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...