Hello,
Can anyone tell me why a javascript function I'm using in one of my surveys works great in the web form but gives me a type error when I submit in the mobile app? Any advice would be much appreciated. Lots of details below.
I'm not great with javascript so my function is probably clunky. It is intended to take the responses from a select multiple question, remove "Other" (if it's in there), sort the rest alphabetically, and return it. It works in survey123 connect and the webform.
function sortString(inputString){
let anArray = inputString.split(",");
let len = anArray.length;
let other = -1;
if (inputString.includes('Other')){
other = anArray.indexOf('Other');
delete anArray[other];
anArray = anArray.sort().splice(0,len-1);
}
else{anArray=anArray.sort();}
return anArray.join(',');
}
This screenshot shows the result if I submit using the web form (works as expected) and the result if I submit using the mobile app (the type error)
Here is an abbreviated version of what my xlsform looks like with only the relevant questions and fields shown.
The idea here is that the user selects from a list in "cleanup_null1". Those selections then get sorted alphabetically (minus "Other" if they selected it) in "cleanup_sorted". Then they enter more information in "cleanup_other1" if they selected "Other". The final result is in "request_cleanup_type" where it either stores the "cleanup_sorted" or a concatenation of "cleanup_sorted" and "cleanup_other1"
The only response that is sent to the source layer is "request_cleanup_type", and the only fields visible to the user are "cleanup_null1" and, if they select "Other" then "cleanup_other1"
type | name | label | calculation | relevant | bind::esri:fieldType |
select_multiple cleanup_type | cleanup_null1 | Request Cleanup Type |
|
| null |
text | cleanup_sorted | sorted | pulldata("@javascript", "functions.js", "sortString", ${cleanup_null1}) |
| null |
text | cleanup_other1 | Request Cleanup Type: Other |
| selected(${cleanup_null1}, 'Other') | null |
text | request_cleanup_type | Requested Cleanup | if(selected(${cleanup_null1},'Other'), concat( ${cleanup_sorted},',',${cleanup_other1}), ${cleanup_sorted}) |
|
Solved! Go to Solution.
Hello @KellyTaylor,
The field app is passing the select_multiple values in as an array so the split() method is throwing the error. Try:
function sortString(input){
let inputString = input.toString();
let anArray = inputString.split(",");
let len = anArray.length;
let other = -1;
if (inputString.includes('Other')){
other = anArray.indexOf('Other');
delete anArray[other];
anArray = anArray.sort().splice(0,len-1);
}
else{anArray=anArray.sort();}
return anArray.join(',');
}
Hello @KellyTaylor,
The field app is passing the select_multiple values in as an array so the split() method is throwing the error. Try:
function sortString(input){
let inputString = input.toString();
let anArray = inputString.split(",");
let len = anArray.length;
let other = -1;
if (inputString.includes('Other')){
other = anArray.indexOf('Other');
delete anArray[other];
anArray = anArray.sort().splice(0,len-1);
}
else{anArray=anArray.sort();}
return anArray.join(',');
}
Thanks so much, that did the trick!