JavaScript pulldata function does not work in published survey

80
1
12-03-2021 10:40 AM
KimberlyMcCallum
New Contributor III

Hi All, I wrote a JavaScript function to return a string list of unique values from responses within a repeat. The idea is to present the user with summary information that they can review prior to form submission. The JS function takes a sum of repeat responses as a concatenated string and returns a string with only the unique values. Everything works fine locally but when I publish the survey, I get NaN returned from the function. 

Here is my function that I have tested in the script editor and verified that it works in Connect.  

function getUniqueValString(inputStringArray){
    // convert to array without the comma at the end and filter unique using index then mash back into a string to show user
    let inputArray = inputStringArray.slice(0, -1).split('*');
    const uniqueValues = inputArray.filter((item, idx) => inputArray.indexOf(item) === idx);
    return uniqueValues.join('<br>');    
}

The function works as expected in Connect but not in the webform as shown in the screenshot attached. Any thoughts? 

 

0 Kudos
1 Reply
DerrickWestoby
Occasional Contributor III

I thought that the javascript extensions didn't work at all in web forms and I went to find you the blog post where I read that awhile back.  

https://community.esri.com/t5/arcgis-survey123-blog/extending-survey123-smart-forms-with-custom-js/b...

I don't remember seeing this part the last time I read it:

When writing your own custom JavaScript functions for execution within your Survey123 form, remember that your code will not run within the context of a web browser; you are limited to JavaScript ES6. You can't use DOM objects, or frameworks like JQuery, Ember, Angular etc. You can't access local files or make asynchronous calls either. Despite all these limitations, there is still quite a bit you can do!

 

Once you have your JavaScript function working, you can publish your survey. Custom JS functions are supported in online surveys as well as in the Survey123 field app. However, keep in mind that JS functions will not execute unless a user is signed in to the Survey123 field app or web app.

 

Also, see this post:

https://community.esri.com/t5/arcgis-survey123-questions/survey123-javascript-pulldata-extension-not...

0 Kudos