Select to view content in your preferred language

How to return all labels from a select multiple question?

2739
3
12-20-2022 11:18 AM
DaveK
by
Frequent Contributor

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, 

0 Kudos
3 Replies
DougBrowning
MVP Esteemed Contributor

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.

0 Kudos
JenniferAcunto
Esri Regular Contributor

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}'), '')))

 

- Jen
leo_SwissGIS
Occasional Contributor

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

  • Create two variable (name_list, label_list), in the calculation Field, use the excel TEXTJOIN function to concatenate the values in the "choices" worksheet with commas, and encapsulate the concatenation with '.
  • Be careful, the list should be the same size.
  • Make sure that the names and labels do not contain commas or quote marks
  • Set the bind::esri:fieldType to null 
=CONCAT("'", TEXTJOIN(",", TRUE, choices!B2:B7), "'")

leo_SwissGIS_1-1744014218097.png

leo_SwissGIS_2-1744015065020.png

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.

leo_SwissGIS_0-1744013171441.png

Result:

As you can see, one of the advantages is that it keeps the selection order:

SelectMultiple_Lables.gif

Considerations:

  • I haven't tested with large lists, but I'd be careful regarding performances
  • Javascript function are not supported for public survey
  • You can use this method with several langages, you would need to add an intermediate variable to concatenate the labels in the other language and then use an additionnal field using pulldata("@property", 'language') and a if condition to store the labels in the correct language.
  • You could also adjust the script to have a different separator: just add the separator in the join() method.