Labels for Multiple Choice Questions Using Arcade

6345
20
10-10-2018 11:15 AM
JamesTedrick
Esri Esteemed Contributor
7 20 6,345

When you have a multiple choice question in a survey, have you wondered why the data in a popup looks different than in the form?

When Survey123 publishes a single choice question, by default it takes advantage of geodatabase domains to write the default label into the feature layer as well – this allows the label to appear automatically in popups in the Map Viewer and other applications in your ArcGIS Organization. 

With multiple choice questions, this is more difficult.  Survey123 stores multiple choice questions as comma-separated values in a text field.  That means there isn’t a built-in way to view labels for these questions. 

However, Arcade can be used to take the values stored and substitute appropriate labels.  Arcade is a cross-platform expression language for ArcGIS, and is currently supported in web map pop-ups and ArcGIS Pro.  Previously, Carmel Connolly showed how to use Arcade with ‘other’ choices in questions.  For multiple choice label substitution, a template attribute expression is available at the Esri’s Arcade Expressions GitHub repository; you will need to enter in: 

  • The name of multiple-choice question’s field 
  • The list of values and labels to be substituted 

// Replace the field name here 
var fieldName = "like_fruits";
 
// Replace the choices here 
var choices = {}; 
choices['apple'] = 'Apple   '; 
choices['banana'] = 'Banana   '; 
choices['grapes'] = 'Grapes   '; 
choices['kiwi'] = 'Kiwi   '; 
choices['orange'] = 'Orange   '; 
choices['peach'] = 'Peach   '; 
choices['strawberry'] = 'Strawberry   '; 

function formatLabels(inText) { 
    return Concatenate("", inText); 
} 

function formatMulti(inField, d) { 
    var values = Split(inField, ','); 
    var labels = []; 
    for (var i in values){ 
        labels[i] = formatLabels(d[values[i]]); 
    } 
    var outText = Concatenate(labels, TextFormatting.NewLine); 
    return outText; 
} 

formatMulti($feature[fieldName], choices);


You can see this expression in this webmap.

20 Comments
CarmelConnolly
Esri Regular Contributor

Ohhhh that's so clever!

We had our Esri UK Scottish Conference on Tuesday just gone and had queries about showing multiple select answers so I'll be directing people this way!

Carmel

ArnaudDemontis
New Contributor II

Good, many thanks. This is the solution for me.

Zach_Robinson
New Contributor III

Hi James, I tried implementing this for one of my COVID webmaps. I keep running into this error: 

Execution Error:Field not Found

I can call the field outside of the function so I can't figure out why the expression says it can't find the field. Any thoughts?

Here is my code

var fieldName = "essential_services";
var choices = {}; 
choices['hotline'] = 'COVID-19 Hotline for customer support'; 
choices['curbside_pickup'] = 'Curbside pickup'; 
choices['delivery'] = 'Delivery'; 
choices['emergency_childcare'] = 'Emergency childcare'; 
choices['food_services_groceries'] = 'Food services/Groceries'; 
choices['free_flu_shots'] = 'Free Flu Shots'; 
choices['workshops'] = 'Online workshops/training/tutorials for COVID-19 support'; 
choices['special_hours'] = 'Special hours for seniors or other vulnerable populations'; 
choices['medical_care'] = 'Urgent medical care'; 
choices['wellness_checks'] = 'Wellness checks for vulnerable populations'; 
choices['services_other'] = 'Other essential services';
function formatLabels(inText) { 
 return Concatenate("✓", inText); 
}
function formatMulti(inField, d) { 
 var values = Split(inField, ','); 
 var labels = []; 
 for (var i in values){ 
 labels[i] = formatLabels(d[values[i]]); 
 } 
 var outText = Concatenate(labels, TextFormatting.NewLine); 
 return outText; 
}
formatMulti($feature[fieldName], choices);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JamesTedrick
Esri Esteemed Contributor

Hi Zachary,

The one thing I would check is the exact naming of the field (upper/lower case)- perhaps put it into the arcade script through the tools in the Arcade editor? 

Zach_Robinson
New Contributor III

Hi James, thanks for your reply. I double-checked the field info on the feature service and it checks out okay:

essential_services (type: esriFieldTypeString, alias: What essential services does your business provide?, SQL Type: sqlTypeOther, length: 1000, nullable: true, editable: true)

Calling the field in the Arcade editor outside of the function also works. Do you have any other ideas why this might be happening?

 

MayaCorrie
New Contributor

Hi Zachary,

Did you ever figure this out? I'm getting the same error on my field name.

Thanks,

Maya

Zach_Robinson
New Contributor III

Hi Maya,

Unfortunately I wasn't able to figure out the issue. Our webmap is just listing the multiple choice answers. It's not very elegant but it's what I was stuck with. Let me know if you have any luck. 

MayaCorrie
New Contributor

Thanks Zachary. Will do.

StephenChignell1
New Contributor

Hi all, I'm having the same issue with the error. I've tried every possible workaround I can think of. James Tedrick‌ do you have any suggestions?

MayaCorrie
New Contributor

Hi Zachary,

We were able to resolve the issue by recreating our survey in Survey123 Connect rather than through the web browser. I should also say that I never was able to get the expression to work using my account, but my colleague got it to work through his account. So it could be some oddity related to your login as well. I still don't understand what might be the problem there.

Hope that's helpful.

Best,

Maya

by Anonymous User
Not applicable

Hi everyone,

I had the same issue with getting Execution Error message, but I resolved it and I *think* I know why. I went into Survey123 Connect, opened up the XLS form, went to the choices tab and double checked my values under the 'name' column. I then re-entered all of those into the choices brackets by copying and pasting them from the XLS form. I think I must have made an error in transcribing one or more of my response options. I am not 100% sure why this worked, but that's what I did differently this time.

Oddly, when I was getting the error message, it was related to the last line of code and i'm not sure why updating the choices options fixed it. Below is my code (modified only slightly from the example provided above) in case it helps - if anyone can explain why this works for me now, I am curious: 

// Replace the field name here
var fieldName = "field_24";

// Replace the choices here
var choices = {};
choices['plastic_bottles'] = 'Plastic Bottles ';
choices['plastic_straws'] = 'Plastic Straws ';
choices['plastic_bags'] = 'Plastic Bags ';
choices['aluminum_cans'] = 'Aluminum Cans ';
choices['styrofoam_'] = 'Styrofoam ';
choices['tires'] = 'Tires ' ;
choices['glass_bottles'] = 'Glass Bottles' ;
choices['coffee_cups'] = 'Coffee Cups' ;
choices['other'] = 'Other ' ;
function formatLabels(inText) {
return Concatenate("✓", inText);
}
function formatMulti(inField, d) {
var values = Split(inField, ',');
var labels = [];
for (var i in values){
labels = formatLabels(d[values]);
}
var outText = Concatenate(labels, TextFormatting.NewLine);
return outText;
}

formatMulti($feature[fieldName], choices);

LarryGaudieri1
New Contributor III

Sorry, after exhaustive searching, this really had me hoping. But, after a test, I now understand why this thread is still incomplete:

  1. Survey123's designed in Survey123 Connect will accommodate the Arcade expression and work.
  2. Survey123's designed in Survey12 web-designed, will not. Even if you have everything exact in syntax, it will not find your field on a 'Test'.

This is so frustrating. To add to that, if you want to cache...web-designer and not Survey123 Connect. So, to achieve caching (this was the high priority concern over nicely read labeling) one must:

  • Use the web-designer but suffer as a consequence multiple-choice questions (even if the respondent only selects one out of many) only passing the coded values and not the labels (which, no spaces and other illegal characters are enforced) so you have to use the Arcade expression if you have any outputs of data.
  • Lose caching, abandon the web-designer, and use Survey123 Connect to fix the code-label issue but make respondents (who sometimes fill out multiple surveys a day, all week) repeatedly have to fill in their organization information, emails, and other information.

These offsets between what's supposed to be the same product is actually diverging away from each other. ESRI, what, if anything, do you plan to do to resolve these offsetting negatives forcing the loss of essential functions needed in a survey to begin with?

Thanks for hearing me out. 

Larry

andreoudi
New Contributor III

Hi!

I am using Survey123 connect which includes a multiple choice field and I want to use arcade and the script provided by @JamesTedrick to create a popup. When using the script above I get the same error message as @Zach_Robinson 

"Field not found".

As Zach mentioned earlier I also tried to get the field name with the help of arcade editor without any help. I used the copy/paste function to paste all the values from Survey123 connect.

Where kan the problem be? Does anyone figured this out?

andreoudi
New Contributor III

Hi!

I have now solved this. The skript works with the latest Survey 123 version (3.13.234) but not, in my case, with the previous one.

KevinRobert1
New Contributor III

Just curious if anyone tried to reproduce this Arcade code in a Dashboard List?

ClaudiaGIS
New Contributor III

Hi, I am also interested in doing this in a dashboard list. @KevinRobert1 were you able to figure it out? 

lourymello1
New Contributor II

Hi!

I am trying to replace a label name with @JamesTedrick  script on the arcgis plataform. But 2 diferrents things are happening:

1- First, when I publish survey form and colleted data there are created 4 arquives, one webmap, one feature layer, one form and one feature layer(stakeholder). I am not sure what is that stakeholder but it works as a mirror from feature. If any one could explain me what is that aboult I will be really apreciated!! Then, when I try to use the arcade it is just habilitaded on stakeholder, and not in feature layer. Any one knows why that is happening? 

lourymello1_1-1659445295025.png

2- Second, when I try to use the arcade on stakeholder where the option it is habilitaded the script works on test but when it started to load something happen and all collumn values desapered and now when I try again to run the script shows me an error: 

lourymello1_2-1659445859546.png

 

I am not sure if I explain right. Let me know if I need to leave here any more information.

If any one could help me, I will be really apreciated!!!

Thank you every body!!

 

 

 

BrittanyBurson
Occasional Contributor III

I can't seem to get this to work either. My survey was published using the latest 3.16 connect, but using a feature class hosted in Enterprise 10.8.1.  All my checklist values pasted from the "choices" tab in the XLSX.

I too am getting the field not found error...

 

BrittanyBurson_0-1669744193733.png

 

 

BrittanyBurson
Occasional Contributor III

Update to Field not Found error...

it appears this Arcade snippet works only on the _stakeholder (now "_results" in 3.16) layer that is auto generated by Survey123

BrittanyBurson_0-1669744684354.png

 

MorganZondervan
New Contributor

@JamesTedrick  Is there a way to account for no value being chosen from the multiple selection?  For example, if the respondent did not answer the question "which fruits do you like?" and submitted the survey, is there a way to return "None selected" in the pop up?

Editing to say that Chat GPT was able to help me out.  I added the following statement at the end of the code:

if (IsEmpty(fieldName)) {
return 'None Specified';
} else {
var outText = formatMulti(fieldName, choices);
return outText;
}

About the Author
I work on the Survey123 team. Please note that I do not use the private messages feature in Esri Community; if you need to reach me, feel free to e-mail jtedrick@esri.com .