CASE: array of questions with checks

We might wanna create a whole list of questions or checks that an user needs to run by:

In our code however, we shouldn’t be using a separate code for each question, like this:

{% stripnewlines %}
|
| Question
| Extra
{% newline %}
|----5%----
|----45%---
|----------+
{% newline %}
| {% input custom.checklist.check_1 as:boolean %}
| {% t "Has every invoice been delivered by the customer?" %}
| {% input custom.checklist.extra_info_1 as:text size:mini placeholder:"Extra info" %}
{% newline %}
| {% input custom.checklist.check_2 as:boolean %}
| {% t "Are the sales invoices been booked and finalized?" %}
| {% input custom.checklist.extra_info_2 as:text size:mini placeholder:"Extra info" %}
{% endstripnewlines %}

First of all, it’s way too much work writing your code down like this. you can easily make an error where you use the same object for each question, if you’re not carefull.
Also, imagine a whole bunch of questions have to be added; that’s a lot of writing down extra code!

There’s a way to avoid such thing: you can use an “array” and use that array to create one section of code (instead of code for each question), like this:


{% comment %}
use below code to create an array of questions / checks
- the first part is to create the actual questions, separated by a ";"
- the second part is used to loop over each question / check (separated by a ";")
Want to add questions?? => Be sure to add the question in the first part, and add in the second part an unique word (key) for that question. 
Respect order in both arrays!!
Split on ";"
{% endcomment %} 

{% assign array_checks = "Has every invoice been delivered by the customer?;Are the sales invoices been booked and finalized?;Are the purchase voices been booked and finalized?;Are there any invoices that are related to a previous VAT-period?" | split:";" %}

{% assign array_keys = "delivered;sales_finalized;purchases_finalized;previous_vat" | split:";" %}


<!---------- actual template ----------> 


{% stripnewlines %}
|
|
| {% t "VAT-check" %}
| {% t "Extra comments" %}
{% newline %}
|----5%----
|----5%----
|----35%---
|----------+
{% for check in array_keys %}
{% newline %}
| {% input custom.[check].check_mark as:boolean %}
  {% comment %}Below will make the templates reconciliation needed{% endcomment %}
  {% if custom.[check].check_mark == true %}
    {% assign check_ind = 0 %}
  {% else %}
    {% assign check_ind = 1 %}
  {% endif %}
| {% unreconciled check_ind as:indicator %}
| {{ array_checks[forloop.index0] }} 
| {% input custom.[check].extra_info as:text size:mini placeholder:"Extra info" %}
{% endfor %}
{% endstripnewlines %}


As you might see, we use only one section of specific code to display our checks and comments:

{% newline %}
| {% input custom.[check].check_mark as:boolean %}
  {% comment %}Below will make the templates reconciliation needed{% endcomment %}
  {% if custom.[check].check_mark == true %}
    {% assign check_ind = 0 %}
  {% else %}
    {% assign check_ind = 1 %}
  {% endif %}
| {% unreconciled check_ind as:indicator %}
| {{ array_checks[forloop.index0] }} 
| {% input custom.[check].extra_info as:text size:mini placeholder:"Extra info" %}

Let’s take a deeper look:

We create an array of all our questions we need:

{% assign array_checks = "Has every invoice been delivered by the customer?;Are the sales invoices been booked and finalized?;Are the purchase voices been booked and finalized?;Are there any invoices that are related to a previous VAT-period?" | split:";" %}

Each question is separated by an “;” and we’re going to split on that as well. By splitting it means we’ll be able to loop over that variable (array) and get 4 results (questions).

We create a second array as well, with an unique word or key for each question of the first array:

{% assign array_keys = "delivered;sales_finalized;purchases_finalized;previous_vat" | split:";" %}

Why we do this, becomes clear later on.

We create our header :

{% stripnewlines %}
|
|
| {% t "VAT-check" %}
| {% t "Extra comments" %}
{% newline %}
|----5%----
|----5%----
|----35%---
|----------+
{% endstripnewlines %} 

Now, instead of making code for each question / check, we’re going to use one section of specific code which will display all our question out of our array!
If we were be doing this for example:

{% for item in array_keys %}
{{ item }}
{% endfor %} 

we’ll get this :
10

We are going to use a for-loop to get those values from our array array_keys like this:

{% for check in array_keys %}
{% newline %}
| {% input custom.[check].check_mark as:boolean %}
  {% comment %}Below will make the templates reconciliation needed{% endcomment %}
  {% if custom.[check].check_mark == true %}
    {% assign check_ind = 0 %}
  {% else %}
    {% assign check_ind = 1 %}
  {% endif %}
| {% unreconciled check_ind as:indicator %}
| {{ array_checks[forloop.index0] }} 
| {% input custom.[check].extra_info as:text size:mini placeholder:"Extra info" %}
{% endfor %}

This creates a new line for each element of our array, by only using one section of coding!

{% input custom.[check].check_mark as:boolean %}

In each loop, this custom-object is actually custom.delivered.check_mark, custom.finalized.check_mark, … The [check] makes each input custom object in each loop unique.

{{ array_checks[forloop.index0] }}

By using a forloop.index0 on our array array_checks this wil actually loop through the array along with the original loop.
So the first loop will display “Has every invoice been delivered by the customer?” as object {{ array_checks[forloop.index0] }} ; in the second loop the second question of that array.

The reason we create 2 arrays, is for safety purpose: if we would only use one array and we change the value of an element (because of a typo in a sentence for instance), it’ll cause losing our value we already inputted:

For example: the custom object is called originally custom.Has every invoice been delivered by the cusomer.check_mark and we input some value in it.
Later on, when I change m array and fix the type from cusomer to customer, my object will be changed to custom.Has every invoice been delivered by the customer.check_mark but that’s a different object! The value of the other object won’t be displayed again!

Need to create extra questions?

Only change your 2 arrays; for instance let’s add a new question before the last question:

{% assign array_keys = "delivered;sales_finalized;purchases_finalized;NEW_Q;previous_vat" | split:";" %}

We do the same thing for the other array:

{% assign array_checks = "Has every invoice been delivered by the customer?;Are the sales invoices been booked and finalized?;Are the purchase voices been booked and finalized?;THIS IS A NEW QUESTION?;Are there any invoices that are related to a previous VAT-period?" | split:";" %}

which will result in this now:

Want to change a question

Never change the value of the array array_keys when changing questions; only change the elements of the array array_checks !

3 Likes

Is it also possible to use result tags with array checks?

for example:

We want to make a letter in which we want a certain text to appear when a certain check has the value “Ja”.

the template with the checks contains the following array

{% assign array_keys = “kosten;afhankelijkheid;betalingsachterstand_RSZ;betalingsachterstand_lonen;betalingsachterstand_BV;betalingsachterstand_BTW;betalingsachterstand_VenB;wissels;KTlening;vervalLT;betaling_leningen;nieuwe_kredieten;staatssteun;nalatigheidsintresten;achterstand_fiscaaldossier;omloopsnelheid;belangrijkverlies;sterkafhankelijk;recentverlies;opgesloten;ongunstigewijzigingen;competitief;grondstoffen;verkoopbedrijfsactiva;moedervennootschap;jaarlijkseAV;verlengdboekjaar;rechtsvervolgingen;ongunstigegevolgen;Ondernemingsrechtbank?;beslag;bestuurdersontslagen?;commissarisontslagen?;verlieswettelijkerechten;afwezigheden;sleutelpersoneel;Socialeonrust;nakomenverplichtingen;vernieuwingvergunning;schadegevallen;milieu” | split:“;” %}

So if a check is answered “Ja”, a certain text must appear in the letter, depending on what kind of check is answered “Ja”.

The code below is part of the checklist code

{% for check in array_keys %}
{% newline %}
|{{ array_checks[forloop.index0] }}
|{% input custom.[check].ja_neen as:select options:“ja|neen|nvt” default:default_answer %}

Hi Alexander,

Do you mean you want to get the result of the input variable custom.[check].ja_neen in another template?

If so you can request it with the following code (example for the first part of your array_keys):

period.reconciliations.your_handle.custom.kosten.ja_neen

Kind regards,

Michiel

Hi Michiel,

I want the following result:
for example,

In the custom template “Check WCO” I’ve awnsered yes on “Staat de onderneming hogere financiële kosten toe dan normaal?”

In a custom text “brief WCO” I want to give the following result if awnsered “ja” for the question “Staat de onderneming hogere financiële kosten toe dan normaal?”:
“Uw onderneming onderneming staat hogere financiële kosten toe dan normaal.”

same principle for the next 33 questions

{% assign array_keys = “kosten;afhankelijkheid;betalingsachterstand_RSZ;betalingsachterstand_lonen;betalingsachterstand_BV;betalingsachterstand_BTW;betalingsachterstand_VenB;wissels;KTlening;vervalLT;betaling_leningen;nieuwe_kredieten;staatssteun;nalatigheidsintresten;achterstand_fiscaaldossier;omloopsnelheid;belangrijkverlies;sterkafhankelijk;recentverlies;opgesloten;ongunstigewijzigingen;competitief;grondstoffen;verkoopbedrijfsactiva;moedervennootschap;jaarlijkseAV;verlengdboekjaar;rechtsvervolgingen;ongunstigegevolgen;Ondernemingsrechtbank?;beslag;bestuurdersontslagen?;commissarisontslagen?;verlieswettelijkerechten;afwezigheden;sleutelpersoneel;Socialeonrust;nakomenverplichtingen;vernieuwingvergunning;schadegevallen;milieu” | split:";" %}

If not clear, can we perhaps set up a call to clarify this?

regards

Hi Alexander,

As mentioned in my last reply you could use the following for all questions (“kosten” ofcourse should be replaced for each question)

{% if period.reconciliations.your_handle.custom.[kosten].ja_neen == "ja" %}

you can also define the array and loop over it

{% for check in array_keys %}
  {% if period.reconciliations.your_handle.custom.[check].ja_neen == "ja" %}
     ...
  {% endif %}
{% endfor %}

Hope this helps you?

Regards,
Michiel

Michiel,

It does the trick!
Thanks!