*Please note this solution only works for forms set to organisation and not public due to the javascript query*
In my survey form, when the user selects thier project name {projectname}, I want the form to fill in the corresponding project ID {uniqueID} by requesting this from an online feature layer with this project information.
To do this, I am trying to write a javascript function to query a feature layer using a pulldata calculation and I have reached a blocker. I'm not familiar with javascript so it could be that my syntax is incorrect. I have adapted a javascript function which is provided in the sample survey sheet 'Javascript examples' (see attached screenshot). I am using the 'working with a feature service' example, and the javascript 'url_requests', "fieldValueByLocation" - which I have adapted because I don't want to query by location, I only want to return a value based on the previous answer {projectname}.
Please see the attached xlsx file with the questions for this part of the form, and the javascipt function which I have adapated below.
Pulldata calculation:
pulldata("@javascript", "url_requests.js", "findCode2", ${adminURL}, string(${projectname}), 'uniqueID')
Javascript function:
}
// Query a feature layer and returns a specific field value from
// the feature based on the user input in a previous question
function findCode2(layerURL, userinput, field, token) {
// Output value. Initially set to an empty string (XLSForm null)
let outValue = "";
// Check to make sure both layerURL and location are provided
if (layerURL == null || layerURL === "") {
// The function can't go forward; exit with the empty value
}
// We need the user input for the project name for the query
// Note that I'm using the relatively new ` ` string that lets me place variables ${var}
let project = userinput(" ");
let projectuserinput = `${project}`;
// Set up query parameters
let f = "f=pjson";
let where = `where=${projectuserinput}`;
let geometry = "geometry=false";
let geometryType = "geometryType=esriGeometryEnvelope";
let inSR = "inSR=";
let spatialRel = "spatialRel=esriSpatialRelIntersects";
let outFields = `outFields=${field}`;
let returnGeometry = "returnGeometry=false";
let returnCount = "returnCount=false";
let parameters = [f,where,geometry,geometryType,inSR,spatialRel,outFields,returnGeometry,returnCount].join("&");
if (token) {
parameters = parameters + `&token=${token}`;
}
let url = `${layerURL}/query?${parameters}`;
// return url;
// Create the request object
let xhr = new XMLHttpRequest();
// Make the request. Note the 3rd parameter, which makes this a synchronous request
xhr.open("GET", url, false);
xhr.send();
// Process the result
// This an abbreviated version without being able to distinguish different types of errors
if (xhr.status === 200) {
let response = JSON.parse(xhr.responseText);
if (!response.error) {
if (response.features[0]) {
outValue = response.features[0].attributes[field];
}
}
}
return outValue;
}
Hoping that someone is able to help with this.
Best wishes,
Heather.