Home > Domino Tips > Developer > LotusScript > Creating and adding to multi-value fields within LotusScript
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

LOTUSSCRIPT

Creating and adding to multi-value fields within LotusScript


Chuck Connell
01.02.2007
Rating: -3.05- (out of 5)


Lotus Notes and Domino tips, tutorials and how-to articles
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


VIEW MEMBER FEEDACK TO THIS TIP

Multi-value fields are one of the core features of Lotus Notes and Domino. Creating these fields on a form is simple within Domino Designer. Once a field is defined as multi-value, it is easy for users to add a value to the field -- just type a comma or semicolon, and then enter the new value. But performing the same operation within Lotuscript is tricky, especially if the field is not text type. This tip shows you how to do just that.

If the field in question is text, adding to it from Lotuscript is straightforward, using a method that is designed specifically for this task. Consider this code:

The LotusScript code snippet assumes that you have already initialized the NotesDocument object named ThisDoc. The code then checks to see if the designated field exists. If it does, it calls the method NotesItem.AppendToTextList, to add a new name to the end of the field. If the field does not exist, the code creates it with an initial value, using NotesDocument.ReplaceItemValue.

The LotusScript code for text fields is so simple that it is not worth writing a technical tip about. But suppose you want to do the same thing for a time-date or number field? This should be easy, with methods called NotesItem.AppendToTimeField and AppendToNumberField. Unfortunately, these methods do not exist.

Here is the same code snippet, but modified to handle time-date fields:

Adding to a time-date multi-value field must account for several situations. First, the field may not exist, just as with text fields. Next, the field may exist, but contain something other than a time-date value. This is actually quite common, since Lotus Notes has an annoying habit of initializing time-date and number fields to the null string before you enter a real value.

If you try to append a new value to a time-date field that contains a null string, you will have a lot of problems, since the existing value is not ...


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
LotusScript
LotusScript finds the first occurrence of a string from the right
Clear Recent Contacts view and prevent repopulation in Lotus Notes 8.x
Search Microsoft Active Directory with LotusScript
Three steps to trap and handle save conflicts with LotusScript
Troubleshoot agents by displaying LotusScript variables online
LotusScript sorts lists alphabetically
LotusScript code rebuilds corrupted busytime.nsf file
Soft-code item names to facilitate LotusScript management
LotusScript agent automates selective mail file replication
LotusScript filters and attaches files to a Notes form

LotusScript
LotusScript finds the first occurrence of a string from the right
Clear Recent Contacts view and prevent repopulation in Lotus Notes 8.x
Search Microsoft Active Directory with LotusScript
Three steps to trap and handle save conflicts with LotusScript
Troubleshoot agents by displaying LotusScript variables online
LotusScript sorts lists alphabetically
Run or restart Notes/Domino agents via text messages
LotusScript code rebuilds corrupted busytime.nsf file
Soft-code item names to facilitate LotusScript management
LotusScript agent automates selective mail file replication

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


a valid time-date. And we need code for the normal case, an existing field with legal values in it.

The LotusScript code sample above assumes that the time variable NewTime is initialized with a valid time, perhaps with the statement NewTime=Now.

The central part of the code is the Forall loop, which reads each value in the existing field and adds it to an array. Because we don't know the number of values in the field, we must Redim the array for each new value, being careful to use the Preserve option (so values already in the array are not lost). After we have built an array with the current field contents, we then add the new value to the end of the array. (With another Redim, of course.)

Finally, we use NotesDocument.ReplaceItemValue to rewrite the whole field with the updated contents. This method will set the datatype of the field correctly, based on the type of array being used.

The end of the snippet shows two Else clauses that handle the other cases: field does not exist, and field exists with wrong datatype. In both cases, the field is replaced with the single new time-date value.

Handling multi-value number fields is identical to time-date fields, except that the two variables named New* are changed to Long datatype. The field that holds the existing values (Old*List) can remain a Variant, since a Variant can hold a list of numbers retrieved from a field.

About the author: Chuck Connell is president of CHC-3 Consulting, which helps organizations with all aspects of Lotus Notes and Domino.

MEMBER FEEDBACK TO THIS TIP

I have done this with a bit more efficiency. Do you remember the doc.getitemvalue method? It is a more efficient way of retrieving the array than using a forall loop.
—Ronald V.

******************************************

I have a very slight variation to this tip. I use Ubound to check for the upperbound value. Now, you only have to use Redim once. I would also likely use ReplaceItemValue instead of the abbreviated syntax.
—Bryan P.

******************************************

You can replace the following code: With this code:
—Yuthana S.

******************************************

Unless I'm mistaken, I believe Chuck forgot to set the value of "newtime" before adding it to the array. As a result, this will probably fail.
—Andrew P.

Do you have comments on this tip? Let us know.

Related information from SearchDomino.com:

  • Expert Advice: Using a multi-value field in DECS
  • Looking for a value in a multi-valued text field
  • LotusScript Learning Guide
  • FAQ: LotusScript
  • LotusScript Reference Center

    Please let others know how useful this tip is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our tip contest and you could win a prize.



    Rate this Tip
    To rate tips, you must be a member of SearchDomino.com.
    Register now to start rating these tips. Log in if you are already a member.


    Submit a Tip




    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
    HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts