JS script for dummy!

484
2
Jump to solution
12-03-2020 07:37 AM
ChrisMaclaurin
New Contributor III

Is there a simple JS script example (without feature by location) which queries a feature layer and returns all the attributes / records of a layer? and even better what would be the syntax for finding a Max value of an attribute in the host layer? Any help wold be blissfully received.

Tags (3)
0 Kudos
2 Solutions

Accepted Solutions
IsmaelChivite
Esri Frequent Contributor

Hi. Try this. The function below returns the max value found in the layer and attribute you specify.

If your layer is private, you will need to pass a token from your XLSForm, which you can get with pulldata("@property","token")

 

function getMax(token) {


	var fl ='https://services2.arcgis.com/fJJEXNgxjn0dpNsi/ArcGIS/rest/services/New_Zealand/FeatureServer/0';

	var field = 'OBJECTID';


	var URL = fl + '/query?where=1=1&outStatistics=[{"statisticType":"MAX","onStatisticField":"' + field + '","outStatisticFieldName":"MAX"}]&f=json'


	var xmlhttp = new XMLHttpRequest();


	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 {

			if (responseJSON.features[0]){

				return JSON.stringify(responseJSON.features[0].attributes.MAX);

			}

			else{

				return "No Features Found";

			}

		}

	}
}

 

 

View solution in original post

ChrisMaclaurin
New Contributor III

You are a true scholar and gent.. thank you Ismael :) This works a treat. I have also applied to this now to my desired hosted layer and pulls back the correct (max) value. I found it difficult to find such an example so your help is much appreciated here and I hope others too will find this just as useful. Now my crisis is over I will hopefully also use your example to build on my learning of JS script and applications in survey forms. Thank you once again. Best Regards Chris :grinning_face:

View solution in original post

2 Replies
IsmaelChivite
Esri Frequent Contributor

Hi. Try this. The function below returns the max value found in the layer and attribute you specify.

If your layer is private, you will need to pass a token from your XLSForm, which you can get with pulldata("@property","token")

 

function getMax(token) {


	var fl ='https://services2.arcgis.com/fJJEXNgxjn0dpNsi/ArcGIS/rest/services/New_Zealand/FeatureServer/0';

	var field = 'OBJECTID';


	var URL = fl + '/query?where=1=1&outStatistics=[{"statisticType":"MAX","onStatisticField":"' + field + '","outStatisticFieldName":"MAX"}]&f=json'


	var xmlhttp = new XMLHttpRequest();


	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 {

			if (responseJSON.features[0]){

				return JSON.stringify(responseJSON.features[0].attributes.MAX);

			}

			else{

				return "No Features Found";

			}

		}

	}
}

 

 

ChrisMaclaurin
New Contributor III

You are a true scholar and gent.. thank you Ismael :) This works a treat. I have also applied to this now to my desired hosted layer and pulls back the correct (max) value. I found it difficult to find such an example so your help is much appreciated here and I hope others too will find this just as useful. Now my crisis is over I will hopefully also use your example to build on my learning of JS script and applications in survey forms. Thank you once again. Best Regards Chris :grinning_face: