1

I'm creating a survey where the collector can drop a point and several fields auto-populate from another feature layer. For example, an inspector is at a property and finds a code violation. Hence, they drop a point, and the parcel ID, owner, and owner's mailing address auto-populates from the county parcel feature layer. I've read up on using JavaScript and pulldata() and have put together a script based on a template in Survey123.

function featureByLocation(location) {
// 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 === "" || location == null || location === "") {
    // The function can't go forward; exit with the empty value
    return location;
}

// The coordinates will come in as `<lat> <lon> <alt> <acc>`.  
// We need <lon>,<lat> for the query
// Note that I'm using the relatively new ` ` string that lets me place variables ${var}
let coordsArray = location.split(" ");
let coords = `${coordsArray[1]},${coordsArray[0]}`;

var layerURL = 
Feature Service REST Endpoint   

// Set up query parameters
let f = "f=json";
let geometry = `geometry=${coords}`; 
let geometryType = "geometryType=esriGeometryPoint";
let inSR = "inSR=4326";
let spatialRel = "spatialRel=esriSpatialRelIntersects";
let outFields = "outFields=*";
let returnGeometry = "returnGeometry=false";
let returnCount = "returnCount=1";
let parameters = [f,geometry,geometryType,inSR,spatialRel,outFields,returnGeometry,returnCount].join("&");
let url = `${layerURL}/query?${parameters}`;

// 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
if (xhr.readyState === xhr.DONE) {
    if (xhr.status !== 200) {
        // The http request did not succeed
        return "bad request: " + url
    } else {
            // Parse the response into an object
        let response = JSON.parse(xhr.responseText);
        if (response.error) {
            // There was a problem with the query
        } else {
            if (response.features[0]) {
                outValue = JSON.stringify(response.features[0]);
            } else {
                // No features found
            }
        }
    }
}
return outValue;

My output generates the following below:

Survey123 Output

How could I include the Parcel ID, Owner, and Mailing address?

2 Answers 2

0

I am working on something similar. Your code works great except that the outValue is a stringified JSON object, where you probably want to pass an attribute value to your survey field not the whole JSON.

Here's what I changed, in my case I'm looking for the attribute field named DISTRICT:

if (response.features[0]) {
              outValue = response.features[0].attributes.DISTRICT;

If that doesn't work, check your REST query and make sure it is written correctly and is set to output JSON.

0

Although a bit off-topic, please note that you can save yourself some time if you consider using Javascript, that Javascript functions are disabled when used outside your organisation or used on a public (non-login) survey.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.