Bob Balaban's Blog

     
    alt

    Bob Balaban

     

    Geek-o-Terica 11: View AutoUpdate, Part Deux

    Bob Balaban  April 14 2010 06:19:01 PM
    Greetings, Geeks!

    A little over a week ago I posted on the topic of the View.AutoUpdate property, and made some comments about how it affects the results and the performance of agent (or other) code accessing views. I promised a follow-up post delving a bit more deeply into this topic, and here it is.

    I created a form named "Numbers", with a single field on it, of type number, and name "number"
    I created a view (named "Numbers") with a selection formula including all the documents whose forn field is "number", and created 1 column containing the "number" field from the form. I set the column to sort, ascending.

    Then I created 5 documents using the "Numbers" form, with the values: 2, 4, 6, 8 and 10.

    Then I wrote this LotusScript agent:

    option public
    option declare             ' no kittens were harmed in the creation of this agent
    Sub Initialize
            Dim n As Integer
            Dim s As New NotesSession
            Dim db As NotesDatabase
            Dim doc As NotesDocument, tmp As NotesDocument
            Dim v As notesview
           
            Set db = s.currentdatabase
            Set v = db.getview("Numbers")
            v.AutoUpdate = True
            Set doc = v.GetFirstDocument
            While Not (doc Is Nothing)
                    n = doc.number(0)
                    Msgbox "Number is: " & n
                    If n = 4 Then
                            doc.number = 7
                            doc.save True, False
                    End If
                    Set doc = v.getnextdocument(doc)
            Wend
    End Sub

    Note that I'm setting Autoupdate to "True" (it's the default setting anyway). The agent looks at (and prints out) the number value of every document, as accessed in order from top to bottom in the view. When we get to the 2nd document (number = 4), we change the value to 7 and save the document.

    With AutoUpdate set to True, the sequence that gets printed out is: 2, 4, 8, 10. Notice that 6 got skipped, because changing the value of the second document from 4 to 7 moved that document in the sort order AFTER the third document, which contains the value 6. So the new "next" document is the containing 8, because the getNextDocument() logic caused our snapshot of the view to be updated before retrieving the next document in the sequence.

    Then I reset the value of the document now ontaining 7 to 4 again, and re-ran the agent with AutoUpdate set to False. The sequence that gets printed out this time is: 2, 4, 6, 8, 10. At the end of the agent's run, the view contains: 2, 6, 7, 8, 10, as it should. The difference is, that with AutoUpdate off, our snapshot of the view index remains constant after the change and save of the 2nd document, so the "next" one after that is STILL the one containing the 6.

    That's it, in a nutshell. One final comment: Were this a Java agent, you would change the Set doc = v.getnextdocument(doc) line to:
         tmp = v.getNextDocument(doc);
         doc.recycle();
         doc = tmp;

    Geek ya later!

    (Need expert application development architecture/coding help? Contact me at: bbalaban, gmail.com)
    Follow me on Twitter @LooseleafLLC
    This article ┬ęCopyright 2010 by Looseleaf Software LLC, all rights reserved. You may link to this page, but may not copy without prior approval.