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.
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 😀
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"; } } } }
Hi Javier,
The statisticType":"MAX will return the highest value on the named field (KmFin). There is no filtering in the function.
There may be another way but I would filter on a second field by bringing it into your function on the xls form e.g pulldata("@javascript", "functions.js", "NameOfFunction", ${token}, ${SecondField})
This would be brought into the function as an argument eg. function NameOfFunction (token, SecondField) {
Then you would need to know how to filter using JavaScript in the function.
The token does not need to be manually entered, it passes through automatically
Chris
Hello chrisif the IdCar is the name of another field that is in the layer, it is the field I want to use to filter the values, but it keeps returning the maximum value of the KmFin field. it should be in the where class where I have to enter the filter field, but I can't get it to do anything....
I don't get an error in the fx once I paste the token, which provokes a question: Every time I start the survey, do I have to paste the token in the fx for the script to work, or does the program itself update the token once it has been entered?
Greetings javier
Hi JMP,
I don't know IdCar? Is it a field name? I can't see it in your function.
I have never really used the fx area in scripts. It doesn't seem to work for me, yet the form & script are returning me the values I desire in the form I am using so the token does appear to be working ok, but yet it returns me an error in the fx area.
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_0d42ead0e50e46b48f8e1eb32e39aff1/FeatureServer/0';
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
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
good, I can't filter the value through a specific field.
this is my script:
all help is appreciated
greetings
thank you very much!!!!!!!
now I just need to filter the maximum values through a question.
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.
Could be a possible fix, not 100% sure though?
this error 400:
Good afternoon,could you tell me how to enter the token, I have created the field right now I get this error:
that's how I have it in the xls:
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
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.