Hi -
I am hoping to add some functionality into my XLSForm that would prevent data entry if a duplicate Project ID is entered into Survey123. I would like this to happen as soon as the user enters a Project ID into the form as the form is quite long (it would really stink to get to the end and then see this error!).
I feel like a JavaScript function would be the best method for doing this, however I am struggling to find an example and really do not know how to write one myself.
I do not think pulldata() would be a reasonable solution for this as it would be extremely cumbersome to maintain the external CSV - new projects are being entered weekly. The ideal solution would be a way to look back at the hosted feature service, check the field and see if the Project ID already exists.
I am also wondering if the "Unique" setting could be used to accomplish this; I suspect that it would not cause error until going to submit the survey form.
Any advice would be welcome!
Thank you,
Solved! Go to Solution.
Thank you erica_poisson. I am attempting to use this in a survey to collect new features for an internal 'GeoGuessr' style game for GIS Day. When I enter a duplicate value, it does not identify it as being duplicate until the very end of the survey, not at the beginning as anticipated. Everything else is working, but it doesn't provide an error message until the end of the survey. Attached is a sanitized .xlsx to remove our URLs/IDs, etc. and a screenshot of the data for reference and the messages we receive during (after!) entry. I think I'm missing something small/basic somewhere... Thanks for any help!
/*
* JavaScript functions for Survey123
*/
function returnFeatures(map_user, token, debugmode) {
// Output value. Initially set to an empty string (XLSForm null)
let outValue = "";
let layerURL = "https://<our service url>";
let response ="";
// Set up query parameters
let f = "f=json";
let where = `where=map_user='${map_user}'`;
let outFields = "outFields=*";
let returnGeometry = "returnGeometry=false";
let returnCount = "returnCount=1";
let parameters = [f,where,outFields,returnGeometry,returnCount].join("&");
let url = `${layerURL}/query?${parameters}`;
if (token){
url = url + "&token=" + token;
}
// 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
response = JSON.parse(xhr.responseText);
if (response.error) {
return (debugmode? JSON.stringify(response.error):"");
} else {
if (response.features[0]) {
return "That Player name is already taken, please enter a different name."
//JSON.stringify(response.features[0]);
} else {
if (map_user !== "") {
return "Player name entered."}
//(debugmode? "No Features Found":"");
}
}
}
}
}
I'm very sorry for the late reply. This is likely due to order of relevant statements within your Survey123 form. My pulldata() executes and then a series of questions with relevant statements show (or not) and allow a user to proceed (or not) with data entry.