Thursday, August 25, 2011

Select Records by Date in Views without using @Today or @Now

 When you create your view selection formula and you use @Today or @Now, your view index is constantly updated because those functions are always being changed.  A way to do this and gain some performance on your view selection is to programmatically update your view selection.  Use the below code either in the Initialize subroutine or in an agent that runs once a day to "hard-code" your view selections that depend on dates.
 
      Sub Initialize
         ' This script will update a view selection formula with an actual date versus doing the same thing via the @Today
         ' function (which recalculates constantly.
         ' So you go from:
         ' SELECT ((Form = "FormA") | (Form = "FormB")) & (@Created > @Adjust(@Today;0;0;-60;0;0;0))
         'To:
         'SELECT ((Form = "FormA") |  (Form = "FormB"))  &  (@Created > [06/26/2011])
         ' This should improve the performance of this view since your index is not constatnly being recalculated.
   
         Dim ses As New NotesSession
         Dim db As NotesDatabase
         Dim view As NotesView
         Dim formula As String
         Dim fmDate As  Variant
         Dim dateTime As New NotesDateTime( "" )
   
         Set db = ses.CurrentDatabase
         Set view = db.GetView("myView")
   
         dateTime.LSLocalTime = Now - 60
         fmDate = dateTime.DateOnly
         formula = {SELECT ((Form = "FormA") |  (Form = "FormB"))  & } &{ (@Created > [} &  fmDate & {]);}
         view.SelectionFormula = formula
   
     End Sub


Enjoy!

No comments: