skip to Main Content

QUICK XMERGE CODING

splitlines() Using text from a multifield

January 27, 2016 |  matthew-townsend

But multifields already output…

Yes they do output text – but as a list and like so many things in life, requirements and what clients want can be diverse and often not limited to one type of output.

This quick xplain will look at how to extract the respective value and text options from a multi field so that you can then apply them in new and exciting ways but especially ways that avoid the need for hard coding.

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:

Xplan, Multifields, Text and Values
Xplan, Multifields, Text and Values

Text items are what is always displayed on the interface and in your outputs – think of it as the client friendly part of the field.

Value is the behind the scenes data that the system actually relies on.  Generally, you can change text aspects of a field – as long as the values remain the same – without consequence (templates, config pending) but changing the values will actually have an impact on things like interface conditioning or the linking and of course your xmerge.

In xmerge both of these are extractable by just adding .value or .text at the end of the field.

<:=$client.yes_no_maybe:>
# Yes

<:=$client.yes_no_maybe.text:>
# Yes

<:=$client.yes_no_maybe.value:> / <:=$client.valueOf(‘yes_no_maybe’):>
# 1 / 1

However as the name suggestions, multi-fields can have multiple selections and you will find applying the above logic to these fields produces some messy results.  Therefore, we need to do a bit more tweaking to get that sweet sweet client friendly text output.

Extracting Text from a multi

To extract the text selections of a multi field we just need to use the string method ‘splitlines()

Splitlines() returns a list result of each line of a string.  Each selection of the multi field 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.my_multifield_1.text.splitlines():>
<:=item:>
<:end:>

# option 1
# option 3

So now we have a simple basis for extracting the text selections without having to use a lengthy and usually hard coded series of if statements.  We can now take this decoupled text and use it in all manner of ways to deliver the outputs are clients are trying to achieve all without hard coding items and in a concisely coded manner.

About creating multi and choice fields

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!

The value and text attributes can be words or even the exact same thing.  This has the benefit of making your coding so much more intuitive and naturally easier to ready.

Think about a common field like  “who is advice for”, often similar fields exist and are used in wizards to determine if the document is for the Client, Partner or Both.  Now in most cases this field will be set up with numbers…so you picture yourself looking at a document where the conditions are all pointing to obscure values like “1” and “3”…wtf do those mean?  Now chances are you’re going to have to go check the field definitions.  But, if the value had been set as “Client” instead of 1 and “Both” instead of “3” you wouldn’t have had to have checked, it would have been obvious and apparent from the get go!

So set up your fields properly as it will make them innumerably easier to read in the long run for you and other team members.”

That said…

Just because there are good use cases to use text instead of numbers doesn’t mean you don’t use both. Take premium frequency for example:

Using values that align to the raw frequency number actually makes our coding easier.
Value: 52 |  Text: ‘Weekly’.

To work out the annual premium we can then just do simple things like

<:=currency(floatify($client.premium_amount.value*$client.premium_frequency.value),0, has_symbol=True):>

So as always, apply balance and just give some thought when creating your fields to use the best approach.

About the author

Matthew Townsend on EmailMatthew Townsend on Linkedin
Matthew Townsend
Advice Technology Consultant | Digital Advice at Create Something
Matthew is an experienced and innovative Xplan consultant and developer, having worked on and developed some of the largest advice projects in the industry. Passionate about building great experiences in xplan that enable businesses and clients to get the most out of this powerful software.
Back To Top