Stop Survey123 Entry if Duplicate Project ID Entered

361
1
Jump to solution
03-07-2022 01:36 PM
erica_poisson
Regular Contributor

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. 

erica_tefft_0-1646688944871.png

Any advice would be welcome!

Thank you,

Erica
1 Solution

Accepted Solutions
erica_poisson
Regular Contributor

I was able to get some help and figure out how to do this. I wanted to post the result here in case it is helpful to others.

I am using a custom JavaScript function and a series of hidden and relevant questions in the XLSForm to accomplish my goal - prevent the submission of a new Survey123 form if the file number entered is a duplicate value. 

First, here is the JS function:

/*
* JavaScript functions for Survey123
*/

function returnFeatures(DCR_File_Number, token, debugmode) {

    // Output value.  Initially set to an empty string (XLSForm null)
               let outValue = "";

               let layerURL = "https://URL GOES HERE/FeatureServer/0";
               let response ="";
               
               // Set up query parameters
               let f = "f=json";
               let where = `where=DCR_File_Number='${DCR_File_Number}'`; 
               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 "DCR File number is not unique"
                                                                           //JSON.stringify(response.features[0]);
                                                            } else {
                                                                           if (DCR_File_Number !== "") {
                                                                           return "DCR File number is unique"}
                                                                           //(debugmode? "No Features Found":"");
                                                            }
                                             }
                              }
               }
               
}

 

Next, I configured the XLSForm - here is what all of the populated columns look like (I hid all of the empty/non-relevant columns to fit this into one image):

erica_tefft_0-1648470843942.png

When the user goes to enter a file number, if it is invalid (AKA a duplicate), they will see this message in the Survey123 form:

erica_tefft_1-1648470948538.png

Because of line 6 in the XLSForm, they are prevented from submitting their form until they correct the DCR File Number so that it is unique. 

When a unique DCR File Number is entered, this message appears and the user is able to submit:

erica_tefft_2-1648471029609.png

This seems to work very well for my use case. Since this is a web-only form (users only complete from a desktop PC) this works perfectly. 

Erica

View solution in original post

1 Reply
erica_poisson
Regular Contributor

I was able to get some help and figure out how to do this. I wanted to post the result here in case it is helpful to others.

I am using a custom JavaScript function and a series of hidden and relevant questions in the XLSForm to accomplish my goal - prevent the submission of a new Survey123 form if the file number entered is a duplicate value. 

First, here is the JS function:

/*
* JavaScript functions for Survey123
*/

function returnFeatures(DCR_File_Number, token, debugmode) {

    // Output value.  Initially set to an empty string (XLSForm null)
               let outValue = "";

               let layerURL = "https://URL GOES HERE/FeatureServer/0";
               let response ="";
               
               // Set up query parameters
               let f = "f=json";
               let where = `where=DCR_File_Number='${DCR_File_Number}'`; 
               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 "DCR File number is not unique"
                                                                           //JSON.stringify(response.features[0]);
                                                            } else {
                                                                           if (DCR_File_Number !== "") {
                                                                           return "DCR File number is unique"}
                                                                           //(debugmode? "No Features Found":"");
                                                            }
                                             }
                              }
               }
               
}

 

Next, I configured the XLSForm - here is what all of the populated columns look like (I hid all of the empty/non-relevant columns to fit this into one image):

erica_tefft_0-1648470843942.png

When the user goes to enter a file number, if it is invalid (AKA a duplicate), they will see this message in the Survey123 form:

erica_tefft_1-1648470948538.png

Because of line 6 in the XLSForm, they are prevented from submitting their form until they correct the DCR File Number so that it is unique. 

When a unique DCR File Number is entered, this message appears and the user is able to submit:

erica_tefft_2-1648471029609.png

This seems to work very well for my use case. Since this is a web-only form (users only complete from a desktop PC) this works perfectly. 

Erica