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.
Solved! Go to Solution.
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";
}
}
}
}
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";
}
}
}
}
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 😀
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
this error 400:
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?
thank you very much!!!!!!!
now I just need to filter the maximum values through a question.
good, I can't filter the value through a specific field.
this is my script:
all help is appreciated
greetings
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
hi Chris
thanks for the help, currently this is the script I have:
function getMax(IdCoche,token) {
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