Hi,
I've been using the Damage Assessment Solutions that includes Survey123 Connect XLS forms. Part of that is the JavaScript functions that should pull data from a feature service and use the data to populate a field in the survey. I keep getting a 400 error saying the URL is invalid. I've even created a URL that I know works and short circuited the JavaScript code and still get the invalid URL error. I've had this working in other surveys, just not the damage assessment.
This is how the function is called from the XLS
pulldata("@javascript","myJSFunctions.js","queryPolygon",string(${location}),"PARCELID",pulldata("@property","token"),true)
Below is the code in the scripts folder.
function queryPolygon(location,fields,token,debugmode){
if (location===""){
return (debugmode? "Location Object is empty":"");
}
var featureLayer = "https://services1.arcgis.com/**********/arcgis/rest/services/RE_PARCELS_WEB/FeatureServer/0";
var coordsArray = location.split(" ");
var coords = coordsArray[1] + "," + coordsArray[0]
var xmlhttp = new XMLHttpRequest();
var url = featureLayer + "/query?geometry=" + coords + "&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=" + fields + "&returnGeometry=false&returnCount=1&f=json"
if (token){
url = url + "&token=" + token;
}
xmlhttp.open("GET",url,false);
xmlhttp.send();
if (xmlhttp.status!==200){
return (debugmode? xmlhttp.status:"");
} else {
var responseJSON=JSON.parse(xmlhttp.responseText)
if (responseJSON.error){
return (debugmode? JSON.stringify(responseJSON.error):"");
} else {
if (responseJSON.features[0]){
return JSON.stringify(responseJSON.features[0]);
}
else{
return (debugmode? "No Features Found":"");
}
}
}
}
This is the error that is stored in the JSON field. See Picture
{"code":400,"message":"Invalid URL","details":["Invalid URL"]}
Then I created a url that I know works and hardcoded it into the code and still get the same error.
function queryPolygon(location,fields,token,debugmode){
if (location===""){
return (debugmode? "Location Object is empty":"");
}
var featureLayer = "https://services1.arcgis.com/***********/arcgis/rest/services/RE_PARCELS_WEB/FeatureServer/0";
var coordsArray = location.split(" ");
var coords = coordsArray[1] + "," + coordsArray[0]
var xmlhttp = new XMLHttpRequest();
var url = featureLayer + "/query?geometry=" + coords + "&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=" + fields + "&returnGeometry=false&returnCount=1&f=json"
//short circut url with known url that works when pasted into browser.
var url = "https://services1.arcgis.com/**********/arcgis/rest/services/RE_PARCELS_WEB/FeatureServer/0/query?geometry=-80.39273,27.21473&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=PARCELID&returnGeometry=false&returnCount=1&f=json"
if (token){
url = url + "&token=" + token;
}
xmlhttp.open("GET",url,false);
xmlhttp.send();
if (xmlhttp.status!==200){
return (debugmode? xmlhttp.status:"");
} else {
var responseJSON=JSON.parse(xmlhttp.responseText)
if (responseJSON.error){
return (debugmode? JSON.stringify(responseJSON.error):"");
} else {
if (responseJSON.features[0]){
return JSON.stringify(responseJSON.features[0]);
}
else{
return (debugmode? "No Features Found":"");
}
}
}
}
Any ideas would be greatly appreciated. As well on what the debug mode is and how would someone debug this code.