Count Number of Features in a feature service

1748
4
Jump to solution
01-14-2022 08:37 AM
RosemaryHatch
New Contributor III

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.

 

RosemaryHatch_0-1642178177539.png

Thank you!

0 Kudos
1 Solution

Accepted Solutions
IsmaelChivite
Esri Notable Contributor

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:

View solution in original post

4 Replies
IsmaelChivite
Esri Notable Contributor

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:

JerrySneary
New Contributor III

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

0 Kudos
IsmaelChivite
Esri Notable Contributor

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:

RosemaryHatch
New Contributor III

This is perfect, thank you!!