API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Delete From List
When dealing with lists (arrays) in formula language, every once in a while you want to remove an entry from the list. There are slick ways of doing this with @Replace or other ways, but those have their drawbacks. For example, with @Replace, if there are duplicate entries in the list both will be removed (that may or may not be desired). This code, although a little longer than those "slick" ways, is pretty easy to follow and removes one and only one element from a list in formula language.

The first thing the code does is define a "placeholder" for the field to be updated. This is not completely necessary in the later versions of Notes, but it's still a good practice and doesn't hurt anything:

FIELD Servers := Servers;

Next, find out which item the user wants to delete (through a prompt) and return if they didn't pick anything:

Item := @Prompt([OkCancelList] : [NoSort]; "Choose Server"; "Choose the server to delete from the list below:"; @Subset(Servers; 1); Servers);
CheckItem := @If(Item = ""; @Return(0); @IsNotMember(Item; Servers); @Return(0); 0);

Next, find out which element was selected, relative to the total number of elements (is it the first one, last one, or one in the middle):

E := @Elements(Servers);
P := @Member(Item; Servers);

Build the new list of servers. If there's only one element (E=1), then the new list is blank. If you're removing the first element (P=1), then keep the 2nd through the last (subset using 1-E, which results in a negative number, so it starts from the last element). If you're removing the last (P=E), then keep the first through the 2nd to last (subset using E-1, which results in a positive number, so it starts from the first element). If you're removing something in the middle, then use two statements and put them back together. The first statement pulls out the first "P-1" elements (everything before the selected item). The second statement pulls out everything after the selected item. This is done through "P-E" which is a negative number (so it starts from the right) of the number of elements after the selected one.

NewServers := @If(E=1; ""; P=1; @Subset(Servers; 1-E); P=E; @Subset(Servers; E-1); @Subset(Servers; P-1) : @Subset(Servers; P-E));

Now store the updated list back in the field:

SaveNewServers := @SetField("Servers"; NewServers);

And that's it. If you're displaying the list in computed text instead of showing editable fields, then you might want to refresh the document and optionally the hide formulas:

@Command([ViewRefreshFields]);
@Command([RefreshHideFormulas])