65. Parsing "Select All That Apply" Answer Selections
Description
How to parse the selected answers of a "Select All That Apply" multiple choice variable.
Code
This computation parses (or separates) the selected answers of a multiple choice variable (when "Select All That Apply" is activated).
HotDocs will always return "Select All That Apply" multiple choice selections in a list format. You can test each item individually to see if it was selected (IF MCVar = "Selection"). But in the rare event that you need to work with each item in isolation, you must resort to the methods described in this computation. One example is if you need to know what the second item selected is. Or a more practical example is a multiple choice variable that contains a list of names: parties to the case. The user selects each name that is a plaintiff, and the selections are then automatically added to a dialog of plaintiffs.
Code
""
SET Temp-n TO COUNT( MCVar )
SET LoopLimit-n[ Temp-n ] TO 1
SET Temp-t TO "«MCVar»"
REPEAT Loop
// Look for a new-line character (list separator)
SET Pos-n TO POSITION( Temp-t, "\r" )
// There was one
IF Pos-n > 0
// everything up to "\r" is the next answer
SET TextVar[ COUNTER ] TO FIRST( Temp-t, Pos-n - 1 )
// remove this answer from the list
SET Temp-t TO LAST( Temp-t, LENGTH( Temp-t ) - Pos-n - 1 )
// No "\r," so this is the last answer
ELSE
SET TextVar[ COUNTER ] TO Temp-t
SET LoopLimit-n TO UNANSWERED
END IF
END REPEAT
Explanation
When a multiple choice variable is set to allow more than one answer to be selected ("Select all that apply"), its answers cannot be accessed individually like a repeated variable can (e.g. Var[x]). Instead, HotDocs simply returns all of the answers as a list.
To gain individual access to the selected items you must pass them over into a repeated variable. From the repeated variable you can then reference any of the selected items directly and individually.
For example, if you need to know what the second item selected is, you can copy the selections to a repeated variable and then query RepeatedVar[2] for the answer. Another example is a list of names extracted from a master dialog of names. The user could be queried as to which are parties to the case. The names the user selects can then automatically be added to a dialog of parties.
The trick is in parsing (separating out) the list of multiple choice answers. We do this by first getting a count of the answers selected by using the COUNT( MultipleChoice ) model. Once we have this count we set up a simple loop that will cycle COUNT number of times, each time pulling out one answer. These answers are then placed in the repeated variable RepeatedVar, where they can be accessed individually.
This computation relies on a repeated dialog box, Loop, that contains a single repeated number variable, LoopLimit-n (set its advanced options to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file"). Details on how the loop works are available in Computation 15: Loops Via REPEAT. The computation also needs three temporary variables -- Temp-t, Temp-n and Pos-n, a text and two number variables. Temp-t holds the list returned by MCVar, Temp-n holds the number of answers, and Pos-n is used to store the string position of where elements in the list are separated.
Answers are extracted from the multiple choice variable MCVar and placed in the repeated variable TextVar. This variable must belong to a repeated dialog box.
Each pass of the Loop looks for the position of "\r" in our list, Temp-t. "\r" represents the new-line character. Since HotDocs returns the multiple choice variable in a list format, the answers are separated by a "\r" (which is, by the way, two characters long: a carriage return and a linefeed). Everything up to the "\r" is placed in the current iteration of TextVar and the string Temp-t is shortened by one answer. The computation continues in this manner, taking an answer off of the front of Temp-t and placing it in TextVar until each answer has been taken.
When the computation completes, the answers chosen for MCVar can now each be accessed individually in TextVar[x].