This script allows you to store the total of an Allocation question in a Number question for use in reports, exports or follow-up questions. You can also pipe the total stored in the Number question into follow-up questions using this format: "Earlier, you said that you spent [%Allocation_Grid_Total%] on..."
Detailed Steps
- In your survey, add a Page element.
- Add an Allocation question to the Page element.
- Create a second Page element.
- Add a Number question to the second Page element.
This question will store the Allocation total. You can hide this question later, although you may want to leave it unhidden for testing. - Open the first Page element (the one that contains the Allocation question) for editing and click the Scripting button.
- Copy and paste the script below into the On Complete tab.
- Replace ALLOCATION_GRID with the Allocation question name.
- Replace ALLOCATION_GRID_TOTAL with Number question name.
- Click Save.
- Preview and test your survey.
Script
/* Script Name: How do I record the sum from an Allocation question? (Modern Surveys) To ensure your script works as expected, always copy the original script from the Script Library
found in the Alida Help Center. Failure to use the latest script from the script library may
cause unexpected results. */
const allocItems = response.getDataPoint("ALLOCATION_GRID").getItems();
const allocTotal = response.getDataPoint("ALLOCATION_GRID_TOTAL");
var total = 0;
// Loop through the allocation items and check the amount for each of them
allocItems.forEach(function(item) {
var value = item.get();
// calculates total of all rows
if (value != null) {
total = total + value;
}
//sets total in hidden open end numeric question
allocTotal.set(total);
});
Example
Sample survey structure:
First page with Allocation question:
Second page with Number question:
Comments
0 comments
Article is closed for comments.