It is often necessary to find out how many times (i.e. count) a particular field either meets or does not meet a certain condition within a Repeat. For example you could use a count of instances to determine whether to use a plural in your template or stop a form from submitting until a certain number of people have been appointed to a specific role
Doing this in long-hand script can be daunting, but by the end of this article you should have all the tools to count instances with ease.
To begin, we suggest you have a firm grasp of how to get field values using script, as not all fields can be referenced the same way. You can read up on these methods here.
Counting the number of times an option is selected
The Basic Example - Single Checkbox
In this section we will be using a script field in our form to collect the number of times a repeated Checkbox control with the field name 'Selection_yn' is equal to true.
When using a form with multiple pages this script field should be placed on the same page as the repeat in question. This will ensure that the field updates each time the values of Selection_yn fields change.
For the basic implementation as outlined above we recommend using JavaScript in the Logic tab of the script field.
Under 'Trigger' select 'Javascript' and add the following code in the box:
var rpt = data.Repeat_rpt;
result = rpt !== undefined ? true : false;
Under 'Action', select 'Value' and add the following code in the box:
var rpt = data.Repeat_rpt;
var count = 0;
for (i=0;i<rpt.length;i++){
var input = rpt[i].Selection_yn;
if (input) {count++}
}
value = count;
Once the field is saved, you can then use this field value to run further rules in your form and show and hide content in your document template.
Checkbox Group, Yes / No Radio or Radio Group - Value 'Is Selected'
For these types of fields, there is a slight modification to the basic example above as we have to reference the option directly. For these fields, to check if the option with the value 'Option 1' is selected, the input line would change to:
var input = rpt[i].Selection_cho["Option 1"];
For a checkbox group this would check if the field contains a selected Option 1, allowing Option 3 (for example) to be selected and the count to still be increased.
Checkbox Group, Yes / No Radio or Radio Group - Field Value
For these fields you could also check for the overall field value. This would only increase the count for a checkbox group if only Option 1 was selected in that repeat row, but would act the same for the radios. Here however, we need to change both the input and the condition in the row below from the basic example - replacing them with the following two lines:
var input = rpt[i].Selection_cho.Selection_cho;
if (input === "Option 1") {count++};
Dropdown List
For the dropdown list we also need to look at the field value, but referencing it is slightly different. Here we also need to change both the input and the condition in the row below from the basic example - replacing them with the following two lines:
var input = rpt[i].Selection_cho.value;
if (input === "Option 1") {count++};
Counting the number of times an option is not selected
Checking if an option is not selected is achieved by changing the condition in each of the above examples. In the basic example we would just write the following in place of the condition:
if (!input) {count++}
And when checking for the field value, we would instead replace the condition line with:
if (input !== "Option 1") {count++};
Using this would check that the checkbox group did not only have Option 1 selected, for example.
What if I don't want to save it as a field value?
If you don't want to save the count of instances as a field value, you have a couple of options.
You could use the count variable in the logic block as part of a condition to set the field's value to something else. For example, to determine a plural, you would change the final line of any of the above examples to:
value = count > 1 ? 'true' : 'false';
You could also use the outline of the count script above as part of a larger function in the script editor, remembering that logic script and script editor script involve differing methods of field referencing.
If you have any trouble with this article, you can submit a request at the top of this page.
Comments
0 comments
Article is closed for comments.