API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Only Select One Checkbox
An interesting situation came up in a recent Notes-only project. The user wanted to have a field with 2 options. Either 0 or 1 of those options could be chosen (but not both). This sounds like a radio button, but with a radio button there's no way to "uncheck" it once it is checked. One option would be to include a button to clear the radio button value. But we chose another path.

We decided to put in a check box. The check box could be checked and unchecked at will. The problem is that we had to make sure that both boxes could not be checked at the same time. This was actually pretty easy to do, after we thought about it for a while.

Using the example from the application we were creating, the checkbox was there to indicate if the current task was behind schedule. If unchecked, the task was on schedule. The options were (obviously not this unprofessional) "behind schedule because of me" or "behind schedule because of the customer". Basically, they wanted to know if it was the developer's fault (swamped with other higher priority items) or the customer's fault (vague/incomplete requirements).

So, we have a checkbox field on the form called BehindSchedule. There are two options for the field. The values (what the user's see) don't really matter, but the aliases (what's stored with the document) are Developer and Customer. The field has the option "Refresh fields on keyword change" enabled. That's a key piece. I'll talk about the Input Translation formula in a moment.

BELOW that field is a hidden, computed for display, text field. That field is called PrevBehindSchedule. The formula for that field is BehindSchedule. Basically, this field lets us know when the other field changes. Notes forms are recomputed top to bottom, left to right, so when the form is refreshed because BehindSchedule changes, that field will be different than PrevBehindSchedule (because that field hasn't recomputed yet) so we can tell what changed. That's another key piece.

Finally, the Input Translation formula on BehindSchedule does all the work. What we want to do is check for the time when one checkbox was checked and the other one has just been checked. In that case, we want to keep the one that was just checked and remove the old one. So this keeps one and only one value checked at any time. The formula is:

@If(@Contains(BehindSchedule; "Developer") & @Contains(BehindSchedule; "Customer"); @Trim(@Replace(BehindSchedule; PrevBehindSchedule; "")); BehindSchedule)

This probably needs some explaining. The first part of the statement checks to see if both values are checked. That's the only case we care about. If only one is checked, or both are unchecked, then we leave alone the value (the "else" part says BehindSchedule -- leave the value alone). So, back to the first part of the statement (when both values are checked). The only way this can happen is if the 2nd checkbox was just checked and the form is refreshing because of the field setting "refresh fields on keyword change". In that case, the field PrevBehindSchedule will have what was checked before the document was refreshed -- remember, that field is below and hasn't been recomputed yet. So what we do is take the current value of the field (which is both values checked) and replace what was previously checked (PrevBehindSchedule) and replace it with a empty string. Trimming that eliminates the empty string, so the only value that's left is the one that wasn't previously checked (which, by definition, is the one that was just checked by the user).

Give it a try in a Notes form (we don't have an example here since this is a Notes-specific solution) and see how it works. The checkbox field can be either unchecked or have one item checked - both items cannot be checked at the same time.