Populate select_one with values from JSON

1642
6
07-12-2021 06:33 AM
GeoSolver
New Contributor III

We have several survey forms with individual fields that are populated with the results returned by custom javascript.  We now have a need to populate a select_one dropdown with choices returned from a javascript function.  Is this possible in the latest version of Survey123?

Tags (3)
6 Replies
jcarlson
MVP Esteemed Contributor

It should be, but depends on what your function is returning. Are you able to share your code / xlsform?

In general, if your JS function is returning a simple string or numeric value, you can use pulldata("@javascript", "your-functions.js", "some-function", "parameter1", ... "parameterN") right in the calculation section of the XLSForm. As long as what your function returns corresponds with a defined choice for the question, this should work just fine.

- Josh Carlson
Kendall County GIS
0 Kudos
GeoSolver
New Contributor III

Thanks Josh.  Sorry I should have been more specific.  What I want to do is define the choices for the dropdown dynamically.  For example, in javascript I send the form's geopoint to the ArcGIS REST API, buffer the point 300ft and return all polygons from a feature service that intersect the buffer.  Then I want to define the choices in a dropdown with the attribute values from the returned list of polygons.  All of that already works except for the last part where the dropdown choices are defined.  So far I haven't found any syntax to use in the excel form that will override the choices that are static in the choices tab.

0 Kudos
jcarlson
MVP Esteemed Contributor

Oh! I see, you'd like the list of choices filtered, not just pre-selected. I think the only way to do this is to utilize the choice_filter column. See: https://community.esri.com/t5/arcgis-survey123-blog/survey123-tricks-of-the-trade-choice-filters/ba-...

I haven't combined a JS function and a choice filter before, but this is still doable using a hidden question and a choice filter.

  1. Have you JS function return a comma-delimited string of choices
    1. "choice1,choice4,choice5"
    2. This is the format of a select_multiple
  2. In your choices tab, create a column. I have named mine filt.
  3. Have the values in this column directly correspond to the choices themselves.
    jcarlson_0-1626100941357.png

     

  4. Create a hidden question that pulls in the string from your function. I've called mine strlist. For testing, mine is just a text field, but it works the same when hidden.
  5. In your select_one question, set the choice_filter to selected(${strlist}, filt)
  6. Try it out!

Here's my test. You can see that my options are limited to those items in my string list.

jcarlson_1-1626101193484.png

EDIT: I should say, set that hidden question to a null type so it's not part of the output schema.

- Josh Carlson
Kendall County GIS
0 Kudos
GeoSolver
New Contributor III

That is a slick way to filter a list of choices that already exist.  In my case, I would have to put 100's of thousands of choices into the spreadsheet to cover all the possibilities that are stored in the feature service.  What I am looking to do is dynamically define the choices without having to store any predefined values in the excel choice tab.

0 Kudos
jcarlson
MVP Esteemed Contributor

You'll probably want an external choice list, then. I haven't worked with them much, but perhaps you could export your layer's vales from the relevant column(s) to a CSV?

- Josh Carlson
Kendall County GIS
0 Kudos
JulienCHARBONNAUD
New Contributor III

Hi, do you have find a solution ? 

0 Kudos