Hello,
I'm creating a survey in Survey123 Connect which contains a select multiple question type. I'm using numeric codes for the choice names and standard text labels for the front end selection. I'd like to have these labels returned within a hidden text field so I can then view the labels instead of the codes for future use in dashboards or other apps. I'm using this calculation expression
jr:choice-name(selected-at(${layerlist}, 1), '${layerlist}')
found via this post - https://community.esri.com/t5/arcgis-survey123-questions/select-multiple-list-no-save-label/td-p/113...
I noticed that there is a choice index number within the expressions which specifies which selection to return. I would like to return all of the select multiple selections and not just a single choice. Is it possible to return all of the choices? If so what would I input in the choice index section of the expression? Any help is appreciated.
Thanks,
I never found a way but would love to hear if there is one. I had to list out the total possible options. So if you have a list with 6 options then you write out jr:choice-name(selected-at(${layerlist}, 1), '${layerlist}') from 0 to 5. If there are only 3 then nothing is returned for 4, 5, 6 so it worked out ok using spaces but it could lead to extra spaces or commas. Not awesome but the best I could come up with.
I agree with @DougBrowning that you have to list all the choices out. To combat the issue of extra commas and spaces, I chain multiple if statements together based on the number of selected choices.
I add a hidden integer question that counts the number of selected choices: count-selected(${ind_roles})
Then use that to build out the if statements. (If 3 choices are selected.... if 2 choices are selected..... etc).
if(${ind_roles_ct} = 3, concat(jr:choice-name(selected-at(${ind_roles}, 0), '${ind_roles}'), ', ', jr:choice-name(selected-at(${ind_roles}, 1), '${ind_roles}'), ', ', jr:choice-name(selected-at(${ind_roles},2), '${ind_roles}')),
if(${ind_roles_ct} = 2, concat(jr:choice-name(selected-at(${ind_roles}, 0), '${ind_roles}'), ', ', jr:choice-name(selected-at(${ind_roles}, 1), '${ind_roles}')),if(${ind_roles_ct} = 1, jr:choice-name(selected-at(${ind_roles}, 0), '${ind_roles}'), '')))
Hi ! For anyone looking for a solution, I found one that is convenient, can be reproduced with relative ease and doesn't need to copy multiple time the choices.
It uses a combination of javascript and excel function.
Principle:
The principle is to concatenante the names and labels in intermediate variables, using the excel TEXTJOIN function.
The javascript function builds a dictionnary from the 2 arrays, and loop through the select_multiple answer to return the list of labels.
Step 1: Concatenate the names and labels in variable
=CONCAT("'", TEXTJOIN(",", TRUE, choices!B2:B7), "'")
Step 2: Create the javascript function in connect
Copy the following code in a new javascript function in Survey123 Connect.
function returnMultipleLabels(ans, names,labels){
var ans_arr = ans.toString().split(',');
var names_arr = names.split(',');
var labels_arr = labels.split(',');
var res = [];
// Create a dictionary to map names to labels
var nameToLabelMap = {};
for (var i = 0; i < names_arr.length; i++) {
nameToLabelMap[names_arr[i]] = labels_arr[i];
}
// Iterate over ans_arr and get corresponding labels from the dictionary
for (var i = 0; i < ans_arr.length; i++) {
if (nameToLabelMap.hasOwnProperty(ans_arr[i])) {
res.push(nameToLabelMap[ans_arr[i]]);
}
}
return res.join();
}
Step 3: Retrieve the labels in the final field
In the final field, call the javascript function with the following parameters: select_multiple answer, names concatenation, labels concatenation.
pulldata('@javascript', 'myFunctions.js', 'returnMultipleLabels',${mySelectMultipleQuestion},${choice_name_list},${choice_label_list})
Depending on the size of the list, you might want to increase the bind:esri:fieldLength parameter.
Result:
As you can see, one of the advantages is that it keeps the selection order:
Considerations: