pulldata javascript query a feature layer to return attribute value based on user input in earlier question

4596
7
Jump to solution
02-03-2022 04:38 AM
HeatherBell
Occasional Contributor

*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. 

 

 

 

GIS Analyst @ The Rivers Trust
1 Solution

Accepted Solutions
ZacharySutherby
Esri Regular Contributor

Hello @HeatherBell,

Depending on if you want both the project name and id stored in the feature service the workflow can depend. Attached is a modified form that uses the JavaScript workflow, but do please keep in mind the limitations when using JavaScript functions. 

You could set the label for the choice list to be the project name and set the choice list name as the project ID and when the survey is submitted the project ID would be the value that is stored in the feature service. 

Thank you,
Zach

View solution in original post

7 Replies
ZacharySutherby
Esri Regular Contributor

Hello @HeatherBell

Based on the XLSForm passed along it looks like you don't need a custom JavaScript function. I'm not sure how you have the search appearance configured on your end in the XLSForm passed along the choices worksheet was corrupt. 

Assuming for your projectsinfo choice list you have uniqueID as the name and projectname as the label when a user selects a project the uniqueID will already be stored in the feature service. 

If you wanted other information like the project description, partner, or status then yes a JavaScript function would be appropriate, but if you just want the uniqueID you would already have it from the select_one question. 

Thank you,
Zach
HeatherBell
Occasional Contributor

Hi @ZacharySutherby , 

Thank you for your reply. I have replaced the spreadsheet which wasn't quite right as it included a solution for finding the {uniqueID} which was done using search appearance. I've also included the choices tab this time in the spreadsheet, and attached an image of the javascript error (see image 'example_form_interventions_with_error_code') which I get when trying to use the pull data a using the javascript function.

The reason why I want to use a javascript function is because I would like to make it neater and more user friendly. The other solution means that the user has to pick a unique ID from a list of one drop down (see image 'select_one_solution_with_search_appearance.png'), which looks a bit odd and is an unneccessary step. So what I actually want to do, is that when the user selects the {projectname} using search apearance and list_one, there is a hidden field which pulls the {uniqueID} from the projects feature layer  based on their selection in the {projectname} question (see image 'example_form_interventions_intended_solution_pulldata'). This is then concatenated to produce a longer intervention/asset ID which includes the project ID {uniqueID} for cross reference. 

So I'm wondering how to resolve the javascript error to make this work. It would also be useful to figure this out so, like you say I can pull the data for the other attributes such as the project description.

I hope this makes more sense. 

Thank you, 

Heather. 

 

GIS Analyst @ The Rivers Trust
0 Kudos
ZacharySutherby
Esri Regular Contributor

Hello @HeatherBell,

Depending on if you want both the project name and id stored in the feature service the workflow can depend. Attached is a modified form that uses the JavaScript workflow, but do please keep in mind the limitations when using JavaScript functions. 

You could set the label for the choice list to be the project name and set the choice list name as the project ID and when the survey is submitted the project ID would be the value that is stored in the feature service. 

Thank you,
Zach
HeatherBell
Occasional Contributor

Hi @ZacharySutherby ,

Thank you for the solution. It works perfectly, but like you say I can't use use it in this instance due to the form being set to public and the javascript dependencies. Thank you for your help. I found that making javascript work in a public survey has already been submitted as an idea here, but will not be going ahead due to security considerations. I have submitted an idea for the search function to be expanded from dynamic choice lists to text & other field types in Survey123.

Thank you, 

Heather. 

GIS Analyst @ The Rivers Trust
0 Kudos
Heena1
by
New Contributor III

Hi Zach,

I have tried the same way with our enterprise hosted data. It doesn't work like yours. See attached form.

Would you have any idea?

Thank you,

Heena

0 Kudos
GIS_User214565456
New Contributor

Hi @HeatherBell and @ZacharySutherby ,

I believe that I am trying to do something similar, but I am having trouble somewhere and I can't pinpoint the problem.  

Objective:  Automate the pull of additional attributes based on the project ID pulled.

The user chooses a project using search appearance and list_one.  Once the project is chosen, then I am trying to pull the project title and project amount based on that project selection.  It will run with no errors but doesn't return the project title or the project amount.  

I have attached screenshots of the script, the xls setup, and the example form.  

I would greatly appreciate any help or if you could point me in the right direction that would be great too. 

0 Kudos
HeatherBell
Occasional Contributor

Hi @GIS_User214565456 , 

I didn't use the javascript solution in the end. Please see my other post here: https://community.esri.com/t5/arcgis-survey123-questions/autpopulate-field-based-on-corresponding-at... 

Hope this helps, 

Heather. 

GIS Analyst @ The Rivers Trust
0 Kudos