Hello All,
My Issue:
I am new to JavaScript and I am trying to create an inspection form that will display all of the past inspections entries above the new inspections. I was able to do just this, but I am running into an issue. I want it to grab the newest record, right now my code grabs just the first record in the table. I am thinking there has to be a way to do this using JavaScript. Below is the .js I created for it. Any help would be much appreciated.
How it is currently set up:
I have a feature class hosted on ArcGIS Online that has a table set up to link using the GlobalID of the feature class to a field called StopGlobalID in the table to link between the two. I have posted the .xls file but it does not have the URL because it is private.
Example:
I have 3 records, for one inspection location, in my table from 12/27/2021 - 1/27/2022 - 2/27/2022 it is currently grabbing the information from the record 12/27/2021 to populate all of my information on my form. I want it to grab the information from 2/27/2022.
Code:
/*
* JavaScript functions for Survey123
*/
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
}
// Set up query parameters
let f = "f=pjson";
let where = "where=StopGlobalID="+"'"+userinput+"'";
let outFields = `outFields=`+field;
let returnGeometry = "returnGeometry=false";
let parameters = [f, where, outFields, returnGeometry].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;
}
Thank you in advanced.
File: