Labels for Multiple Choice Questions Using Arcade

1846
12
10-10-2018 11:15 AM
Esri Esteemed Contributor
7 12 1,846

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.

12 Comments
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

New Contributor II

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

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);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
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? 

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?

 

New Contributor

Hi Zachary,

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

Thanks,

Maya

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. 

New Contributor

Thanks Zachary. Will do.

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?

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

New Contributor II

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);

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

About the Author
I work on the Survey123 team.