Grab the newest Record using Survey123

934
3
Jump to solution
03-07-2022 07:57 AM
CHedger1227
New Contributor III

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:

Thank you
1 Solution

Accepted Solutions
TonyContreras_Frisco_TX
Occasional Contributor III

The easiest thing I can think of is to alter the query to get the most recent record based on a date/datetime attribute.

// Set up query parameters
let f = "f=pjson";
let where = "where=StopGlobalID="+"'"+userinput+"'";
let outFields = `outFields=`+field;
let orderByFields = 'orderByFields=<nameOfDateField> DESC';
let resultRecordCount = 'resultRecordCount=1';
let returnGeometry = "returnGeometry=false";
let parameters = [f, where, outFields, orderByFields, resultRecordCount, returnGeometry].join("&");

 

View solution in original post

3 Replies
TonyContreras_Frisco_TX
Occasional Contributor III

The easiest thing I can think of is to alter the query to get the most recent record based on a date/datetime attribute.

// Set up query parameters
let f = "f=pjson";
let where = "where=StopGlobalID="+"'"+userinput+"'";
let outFields = `outFields=`+field;
let orderByFields = 'orderByFields=<nameOfDateField> DESC';
let resultRecordCount = 'resultRecordCount=1';
let returnGeometry = "returnGeometry=false";
let parameters = [f, where, outFields, orderByFields, resultRecordCount, returnGeometry].join("&");

 

CHedger1227
New Contributor III

That is exactly what i am looking for thank you Tony!

Thank you
bsklaohfl
New Contributor III

@CHedger1227 

I realize this post is a bit old, but I am working on a similar workflow and have a question. Does Javascript have to be used to display the previous inspection data? I've been trying to use pulldata and referencing the CSV file which was added to the media folder on Survey123 Connect, but am not sure if this is possible.

And can I ask why you have the feature class and a related table? Why not just use the table from the feature class?


Thanks for any information you can provide!

Brooke

0 Kudos