skip to Main Content

Xplan Quick Xplain

More Function examples:

additional functions

‘Great things are done by a series of small things brought together’ Vincent Van Gogh.

We’ve had a few emails and conversations around the functions tutorial we explored and wrote about here.  One of the main things that keeps coming up, is people want more examples.

That’s understandable, often working with others who aren’t programmers (as is the case for most of us working in the Xplan space) it can sometimes take just the right example or a couple of examples before it starts to stick and the logic behind a concept is understood and readily applied.

To that end, here is another small example of simple function that makes dealing with Boolean fields in particular, easier.

It’s about the output

In a lot of documents, especially advice documents, we often don’t want to see blank values merge out.  For example, a common piece of data to see in an ‘About You’ or ‘Current situation’ section of an SOA, is ‘Does will exist’

Generally this is driven by the ‘will exists’ Boolean field.

If something has been recorded against that field, we will either get a ‘yes’ or ‘no’ accordingly, but what if nothing has been entered?  We get a blank value and nothing merges.

That might be fine for a fact find where the blank space is a prompt for the client to add in the information or the adviser to ask about – but usually not for an advice document.

There’s lots of similar examples like this throughout most Xplan systems and advice documents.  So rather than seeing nothing merge out,  a client might instead want ‘Not disclosed’ to appear (or the line item to just not appear), because if the client has disclosed it, it should be either a yes or no.

Right, but what about the function

So as we know from above and this article here, when dealing with Boolean fields we have 3 states to account for and when its blank we want to see ‘Not disclosed’ appear.  Same is likely true of other items like Health Status, this is a choice field but again we don’t want blank data.

It can be tedious account for those, so let’s use a function to make it easier.

getResponse Function

You can call your function whatever you want, I’ve called it ‘getResponse’ because that’s exactly what we’re doing, we’re calling whatever field we want and asking it to give us a response.  If we don’t get a response then we will output ‘Not disclosed’ (or any other text you want).

<:let getResponse=lambda x: str(‘Not disclosed’) if not str(x.text) else x.text:>

Let’s break it down (colour coded)

  • Our function name is getResponse (try to keep these simple but on point with what the function does or represents)
  • x is just our standard parameter (argument).  X is just waiting to be subbed in for an actual field, which we pass in when we want to call our field.
  • This is the text we want to output if we don’t find any responses, the condition for which is if not str(x.text)
  • if the condition if not str(x.text) fails then it will output the actual data of the field x.text

To call it in the template:

#Not disclosed

So rather than having to do layers of conditioning or even ternary conditioning for each item, we can simply use the above to ensure we always return a response.

Getting back to our Vincent Van Gogh quote at the start of the article –  many small things build to great things.  Small changes to the way you go about coding like this,  all add up, build on and leads to everyone doing more and achieving more with their templates and code.

Back To Top