Xplan Quick Xplain
Getting the ‘text’ out of a multi field
handy and good to know
Multi fields already output the options selected against them, as a bullet point list.
But just like people, often what clients want to see actually merge out in their templates can be a lot more diverse. This article will look at how to extract the respective value and text options from a multi field so that you can then apply them in all the varying ways clients want to see, without using static long-hand code approaches.
Text and Values
Multi fields and Choice fields require both a ‘value’ and ‘text‘ input for each item added to the field.
You can see the respective breakdown on the image below.
Text items are what is always displayed on the interface – the nice and user/client friendly aspect of each item in the field
Values are the behind the scenes data that the system (xplan) actually relies on. Generally, you can change text aspects of an item without consequence (templates, setup pending) but changing the values will actually have an impact on things like conditioning or the linking, in say the case of a strategy group.
In xmerge both of these are extractable by just adding .value or .text at the end of the field.
<:=$client.yes_no_maybe.value:> / <:=$client.valueOf(‘yes_no_maybe’):>
But because multi-fields can have multiple selections you will find applying the above to multi fields produces some messy results so we need to do a bit more tweaking to get that sweet sweet client friendly texty output.
Extract Text from a multi
To extract the text selections of a multi field we just need to add the ‘splitlines()’ method after the .text call.
Splitlines() is actually a str method that returns a list with all the lines of a string. Each selection of the multi essentially has a break or escape character built in, which allows the splitlines to break it down correctly when we iterate over it like below:
<:for item in $client.test_multifield_1.text.splitlines():>
# option 1
# option 3
So now we have a simple basis for extracting the text selections without having to use a lengthy and usually hardcoded, series of if’s. With this simple basis we can use the plain text in any manner, to deliver the outputs are clients are trying to achieve all without hard coding items and in a concisely coded manner.
But its a numbers game…
Often you will see choice and multi fields setup like the screenshot earlier, where the ‘values’ are purely numeric.
It’s important to note they don’t have to be purely numeric.
The values and text can be the exact same thing and in some cases that can make the field and your coding more naturally readable.
When looking at coding you didn’t create that 1, 2, 3 doesn’t mean anything to anyone else, it becomes a lot quicker to identify and read ‘Yes’ or ‘Joint’ than it is to work out what ‘1’, ‘2’, ‘3’ (and any other obscure combination) might mean.
That said…just because it doesn’t need numeric values, doesn’t mean there aren’t cases for it. In some instances, the text is just too long or too obscure to meaningfully use words. In others set ups, numeric values logically make sense and help:
Take premium frequency
Using values that align to the raw frequency number actually makes our coding easier.
Eg 52 | ‘Weekly’.
To work out the annual premium we can then just do simple things like $client.premium_amount.value*$client.premium_frequency.value
So as always, balance and just give some thought when creating your fields to use the best approach.