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)
Loop

(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 ...
   Next
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.

This was last published in October 2001

Dig Deeper on Lotus Notes Domino Administration Tools

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close