Search code examples
lotus-noteslotuslotusscript

Trying to work with a multi-value array in LotusScript and sort of stuck


I have to find a way to store a series of variables - MonthYear (the key) and a counter. The purpose of this is to track the number of documents processed by Month & Year. I was thinking of a list but I am not sure how to save the data so that it is readable and able to be shown in a table at a later date. I thought about using a multi-dimensional array - someArray(1,0 to 1) and ReDim'ing it each time I start a new MonthYear and then save it back to a field on the document but am not sure how that is going to play out. Does anyone have an idea of how I can accomplish this?

The first dimension will be the MonthYear (key) and the second will be a counter that is updated every time a new document is processed.

  1. The key will be based on a field on the document being processed. How can I make sure I am updating the right key/counter combination?

  2. How can I retrieve the existing counter from the field on the document, update the counter and then replace the value?

I thought about just adding a new element (ReDim) every time a document is processed and than somehow adding up all the counters for each key and storing that in an array, but that just seems real messy. There has to be a good way to do this.

Any and all ideas will be greatly appreciated


Solution

  • The simplest structure I can think of is a "List". Here is a quick example that "round trips" values with lists onto a document and back into a list. Cut and paste into an agent to test (don't forget to set the agent property's "Runtime" target to "None")

    Sub Initialize
        Dim session As New notesSession
        Dim counter List As String
        Dim sValue As String
        Dim doc As notesDocument
        Dim itCounters As NotesItem
        Dim db As notesDatabase
        Dim i As Integer
        Dim vResult As Variant
        Dim vValues As Variant
    
        Set db = session.CurrentDatabase
        Set doc = db.CreateDocument
        Set itCounters = doc.ReplaceItemValue("counters","")
    
        counter("201201") = 16
        counter("201202") = 1
        counter("201203") = 10
        counter("201204") = 5
    
        ' print the tags
        Forall k In counter
            Print Listtag(k)
        End Forall
    
        ' adding the values (and tags)
        Forall k In counter
            Print "[" + Listtag(k) + "]:" + counter(Listtag(k))
            sValue = Listtag(k) + "!" + counter(Listtag(k))
            itCounters.AppendToTextList(sValue)
        End Forall
    
        'retrieving
        Erase counter
    
        Set itCounters = doc.GetFirstItem("Counters")
        vValues = itCounters.Values
        For i = 0 To Ubound(vValues)
            vResult = Split(vValues(i), "!")
            counter(vResult(0)) = vResult(1)
        Next
    
        Forall k In counter
            Print "[" + Listtag(k) + "]:" + counter(Listtag(k))
        End Forall
    
    End Sub
    

    The convenient aspect is that you declare each new item as you require (no Dim'ing). And you can access any value by a meaningful key (month and year or whatever). You can use a field name that matches key to load or just keep the values in a multivalue fields and use a specific separator like in the example. Also, when you increment, you don't have to search an array for right value to increment, you reference it based on the tag, and just increment it. I think that's as simple as it can get.

    The Domino designer help has extensive info about lists. If you need to verify the existence of list elements look at the "isElement" function to.