Hello -
I'm trying to generate a unique number for each survey submittal (e.g. 2022-001) and was going to try to use the count of features that were already in the service. My thought is that I would use a custom JS/pull data to query the feature service and count the number of records when the survey is started. Does anyone have an example of how that would look for someone who is very very rough at javascript? I'm attaching what I have right now, which I know is definitely not right.
Thank you!
Solved! Go to Solution.
Hi, I posed a getCount custom JS function here: https://github.com/IsmaelInRedlands/Survey123-Tricks-of-the-Trade/tree/main
This is what it looks like:
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);
}
}
}
For the feature layer parameter, pass the URL including the layer index. For example:
https://services2.arcgis.com/fJJEXNgxjn0dpNsi/ArcGIS/rest/services/Carriage_Trail/FeatureServer/0
If your layer is shared publicly, you do not need to pass the second parameter. To get the token from the user, use:
pulldata("@property","token")
Starting with the 3.16 release, you can also make use of the pulldata("@layer") function. Check:
Hi, I posed a getCount custom JS function here: https://github.com/IsmaelInRedlands/Survey123-Tricks-of-the-Trade/tree/main
This is what it looks like:
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);
}
}
}
For the feature layer parameter, pass the URL including the layer index. For example:
https://services2.arcgis.com/fJJEXNgxjn0dpNsi/ArcGIS/rest/services/Carriage_Trail/FeatureServer/0
If your layer is shared publicly, you do not need to pass the second parameter. To get the token from the user, use:
pulldata("@property","token")
Starting with the 3.16 release, you can also make use of the pulldata("@layer") function. Check:
Hi @IsmaelChivite,
I am able to return a count of all the records, can I return the count of a variable in the attribute table? Like Count how many records are "Apple" or "Orange"?
Jerry
Replace the WHERE statement in the URL as shown below for fruit='apple'
var url = featureLayer + "/query?f=json&where=fruit='apple'&returnCountOnly=true";
Starting with the 3.16 release, you can also make use of the pulldata("@layer") function. Check:
Hi @IsmaelChivite ,
Is it possible to use two fields to get a count? I'm using the syntax below but it only allows me to use one or the other not both at the same time.
var url = featureLayer + "/query?f=json&where=fruit='apple'&where=color='Red'&returnCountOnly=true";
Kind regards,
Jerry
This is perfect, thank you!!