JS script for dummy!

1439
12
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 Notable 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 😀

View solution in original post

12 Replies
IsmaelChivite
Esri Notable 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 😀

JavierCMartínezPrieto
Occasional Contributor

Good afternoon,
could you tell me how to enter the token, I have created the field right now I get this error:

JavierCMartnezPrieto_0-1643218243000.pngJavierCMartnezPrieto_1-1643218265264.png

that's how I have it in the xls:

JavierCMartnezPrieto_2-1643218291501.png

 

 

 

and when I manage to remove it I get an error 400, invalid error.

Could you put the example of how to enter the token and how it has to be written in the XLS.

thank you in advance and greetings

Javier C. Martinez Prieto
0 Kudos
JavierCMartínezPrieto
Occasional Contributor

this error 400:

JavierCMartnezPrieto_4-1643218796583.png

 

Javier C. Martinez Prieto
0 Kudos
ChrisMaclaurin
New Contributor III

If my memory servies me right it might (but haven't read up on the specific error) be an issue with the length of the token. try setting the  bind::esri:fieldLength to something large like 4000. I have also have the $token row bind::esri:fieldType set to null.

Screenshot 2022-01-26 202211.png

Could be a possible fix, not 100% sure though?

 

JavierCMartínezPrieto
Occasional Contributor

thank you very much!!!!!!!

now I just need to filter the maximum values through a question.

 

Javier C. Martinez Prieto
0 Kudos
JavierCMartínezPrieto
Occasional Contributor

good, I can't filter the value through a specific field.

this is my script:

function getMax(IdCoche,token) {
    var field = 'KmFin';
    var params = "/query?";
    params = params + "where=IdCoche='" + IdCoche + "'&outFields=KmFin"
    params = params + "&orderByFields=KmFin ASC&resultRecordCount=KmFin&f=json"
    params = '/query?where=1=1&outStatistics=[{"statisticType":"MAX","onStatisticField":"' + field + '","outStatisticFieldName":"MAX"}]&f=json';
    var URL = fl + params;
    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";
            }
        }
    }
}

 


all help is appreciated

greetings

Javier C. Martinez Prieto
0 Kudos
ChrisMaclaurin
New Contributor III

Hi JMP,


Not sure if this will fix yours but my function doesn't work when token isn't the first argument?

I.e function getMax(token, IdCoche)

Another problem could be you may need to define the argument 'IdCoche' in the function

Var SecondField = "IdCoche"

Not sure though... I haven't scripted for a while and I'm finding I am even forgetting the correct terminology 😞

Good luck

Chris

JavierCMartínezPrieto
Occasional Contributor

hi Chris

thanks for the help, currently this is the script I have:

function getMax(IdCoche,token) {

var fl ='https://services3.arcgis.com/dha0QtUTrPxlyZDZ/arcgis/rest/services/service_0d42ead0e50e46b48f8e1eb32...';

var field = 'KmFin';

var params = "/query?";

var params = "/query?where=IdCoche='" + IdCoche + "'&f=json";

params = params + 'outStatistics=[{"statisticType":"MAX","onStatisticField":"' + field + '","outStatisticFieldName":"MAX"}]';

var URL = fl + params;

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";

}

}

}

}

 

I keep getting the maximum value of the field I specify in var field, but it doesn't filter me, by the IdCar.

the token issue is quite confusing because for it to work you have to enter it in fx in survey, but these are temporary so they can stop working. i think that section is not very well implemented in survey123 connect.

Regards

 

Javier C. Martinez Prieto
0 Kudos