Wednesday, February 8, 2012

Select a date and update your view

Well, it's 2012 and I realized that I've not made any entries for this year.  So, here's my first tip of the year.

If you create a view that selects documents based on date using @Today or @Now, you will constantly be refreshing that view and eating up resources.  Even more, if you have a large number of documents in the database you will be recalculating what is displayed and causing slow performance for your users.  A better way is to update the view selection formula via Lotus Script to have a "static" selection formula -- even though you can prompt the user for input to change it.  Below is a sample script to do this.  As a side note, your users must have the attribute "Create shared folders/views" selected in order to update the view selection formula.

I promise to try and get additional posts done in a little more timely fashion -- even though this blog is only updated "every so often...".  Have a great day!

 %REM
    Agent UpdateViewSelections
***********************************************
    Written  Feb 03, 2012       
***********************************************
    ' 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 = "myForm") & (@Created > @Adjust(@Today;0;0;-60;0;0;0))
    ' To:
    'SELECT (Form = "myForm")  &  (@Created > [06/26/2011])
    ' This should improve the performance of this view since
    ' your index is not constatnly being recalculated.
***********************************************
%END REM
Option Public
Option Declare
%Include "lsconst.lss"

Sub Initialize
    On Error GoTo ErrorHandler
 
    Dim ses As New NotesSession
    Dim db As NotesDatabase
    Dim view As NotesView
    Dim formula As String
    Dim fmMonth As  Variant
    Dim fmYear As Variant
    Dim fmDate As Variant
    Dim getDate As Variant
    Dim wrkDate As Variant 
    Dim workspace As New NotesUIWorkspace
 
    Set db = ses.CurrentDatabase
    Set view = db.GetView("SelectMonth") 

    getDate = workspace.Prompt(Prompt_OKCANCELEDIT,"Select Date For Review","Enter Date (use format MM/DD/YY)","")
    If IsEmpty(getDate) Then Exit Sub 'We canceled or didn't enter anything
    If CDat(getDate) < CDat("01/01/2005") Then GoTo ErrorHandler
 
    wrkDate = CDat(getDate)

    fmMonth = Month(wrkDate)
    fmYear = Year(wrkDate)
    formula = {SELECT Form = "myForm" & } &{ (@Month(ADATE) = } & fmMonth & {) & (@Year(ADATE) = } & fmYear & {) ;}
    view.SelectionFormula = formula
    Call workspace.viewrebuild 'Reset our view and exit 
    Exit Sub

ErrorHandler: 
    MessageBox "The information you entered is not valid", MB_OK, "Did you enter a valid date?"
    Exit Sub
 
End Sub