Saturday, February 1, 2003

Tips and tricks in LotusScript

PROGRAMMING POWER

By Mick Moignard

Long before I was a Lotus Notes geek, I was employed as a mainframe programmer. I wrote applications in PL/I (Programming Language One), which ran on IBM's MVS (Multiple Virtual System) and used database systems like IMS (Infrastructure Managed Services) and DB2. In fact that's where I learned the art of programming, and most of what I learned then about how to go about writing programs is still valid when writing LotusScript code for Notes now. There are, for sure, some differences. The object oriented access to Notes is different, and the data types are certainly somewhat different. So I thought that in this article I'd present a small collection of LotusScript programming tips and tricks I've worked out and remembered over the last couple of years and compare them to "the good old days."

Array lookups

Arrays are some of the most useful things in any programming language for keeping track of lookup values, conversions of coded data to human-readable values for presentations, and so on. Notes, unlike many database systems, actually makes use of this with the concept of multivalue fields. I know it calls them lists, but what is stored is actually an array.

One application I did a year or two back was designed to collect, in a Notes database, the error reports that came from printers around the organization so that we could do an analysis of the different sorts of errors and exceptions that occurred. The printer messages contained the printer name and a code that indicated what the problem was. In my application I had a couple of profile documents, one that held a table of printers and their physical location data, and the other which had a three column table that translated the error code both to text and to a severity code. These tables were actually implemented as multivalue text fields, and as they were static data maintained by an administrator, I felt that the only validation necessary was to ensure that the multivalue fields on each document had the same number of entries.

Back in the past, to look up items in an array, the usual method was to loop around the array with PL/1's equivalent of a for/next loop until we found a value that matched, or if the array was big, to do a binary search on it. It wasn't difficult code, but it had to be typed and tested every time. LotusScript makes doing this completely redundant with the ArrayGetIndex function, which does all that for you. Consider this code fragment:

        index = Arraygetindex(Printerprofdoc.printer_name, doc.subject(0))
        If Isnull(index) Then
                doc.location = "Not in profile"
                doc.printer_site = "Not in profile"
        Else
                doc.location = Printerprofdoc.printer_location(index)  
                doc.printer_site = Printerprofdoc.printer_site(index)
        End If