Survey123 Connect - calculate total records in feature layer and return a value

1219
5
04-18-2022 11:23 AM
BriannaWiddick
Esri Contributor

I am creating workshop registration survey using Survey123 Connect.  What would be ideal is if the survey is able to tell the survey taker how many spots are left in the workshop.  Is there a way in Connect that we can have the survey calculate the amount of records that have been submitted and return a value? for example it could say "there are 23 seats available" when they go to register for the workshop in the survey.  I am not finding anything that could do this calculation within the survey.  Perhaps this is not a capability of Connect and I need to think of another solution.  

0 Kudos
5 Replies
IsmaelChivite
Esri Notable Contributor

Hi. To return the total record count in a layer you would need to use a custom JS function.  Below is an example.  The problem is that custom JS functions do not work if you publish your survey publicly.  It seems like you would not want people to have to login to register for an event. Sharing the code just in case, although it looks like a long shot.

 

 

function getCount(featureLayer,token){
   
	var xmlhttp = new XMLHttpRequest();
	var url = featureLayer + "/query?f=json&where=1=1&returnCountOnly=true";

	if (token){
        url = url + "&token=" + token;
    }

	xmlhttp.open("GET",url,false);
	xmlhttp.send();

	if (xmlhttp.status!==200){
         return (xmlhttp.status);
    } else {
	var responseJSON=JSON.parse(xmlhttp.responseText)
	if (responseJSON.error){
         return (JSON.stringify(responseJSON.error));
       } else {
      	 return JSON.stringify(responseJSON.count);
       }
    }
}

 

 Bel

BriannaWiddick
Esri Contributor

Thank you so much Ismael for your response! that makes sense, I may need to rethink the process. 

0 Kudos
rachelm
New Contributor III

@IsmaelChivite this works great with a proper token, but is there a way to pass a token dynamically after publishing the survey? 

0 Kudos
SabineKrier
New Contributor II

position(..) Returns the index of the current record in a repeat. For more information, see Repeats.

Putting this in the Calculation column with give you an index of all the records you have in the table. Then you could calculate the available seats minus the number of submitted records for a note telling the applicants how many seats are left.

abureaux
MVP Regular Contributor

Sounds to me like this is going beyond what S123 can comfortably do. Personally, I would use S123 to just collect participant info and use a custom platform to initially launch the form (so people know how many seats there are left before filling out the form), and then to process the submission after S123 (some combination of database and automation software).