Help with code for survey in survey123 conect/Ayuda con código de JS para encuesta de survey123

696
6
01-27-2022 05:53 AM
JavierCMartínezPrieto
Occasional Contributor

Greetings all,
I have a JavaScript script for a survey in survey123, this script takes the maximum value of a field of the survey, but I need this maximum value to be filtered by another field of this survey.
The example is the following, I have a field with the kilometres that the vehicles have done and another field where the license plates are, when I run the script it gives me the maximum value in kilometres but I need that maximum value to be the one of a specific license plate and not the maximum of that field.
This would be the script (it is a small modification of one that is already uploaded on the web):

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 welcome.
Greetings to all.

######################ESPAÑOL################################

Saludos a todos,
tengo un script de JavaScript para una encuesta en survey123, este script toma el valor máximo de un campo de la encuesta, pero necesitaría que este valor máximo se filtrara por otro campo de esta encuesta.
el ejemplo es el siguiente, tengo un campo con los kilómetros que han hecho los vehículos y otro campo donde están las matriculas, cuando ejecuto el script me da el valor máximo que hay en Kilómetros pero necesito que ese valor máximo sea el de una matricula especifica y no el máximo de ese campo.
este seria el script(es un pequeña modificación de una que ya esta subido en la web):

SCRIPT EN LA PARTE QUE ESTA EN INGLES

toda ayuda es bien recibida.
Saludos a todos.

Javier C. Martinez Prieto
0 Kudos
6 Replies
HuubZwart
Occasional Contributor

You redefine your params variable which resets your where clause to "1=1", specifying the where clause first will limit your statistics results to that filter, I think you need:

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

 

JavierCMartínezPrieto
Occasional Contributor

it doesn't work I get error 400

Javier C. Martinez Prieto
0 Kudos
HuubZwart
Occasional Contributor

Have you tried to get the correct query in a browser? Your URL looks kinda funcky, I would expect it to end with /FeatureServer/0 or something similar

JavierCMartínezPrieto
Occasional Contributor

it ends up like this, what has happened is that when you paste the script, those ellipses appear, but if you click on the link, you can see the complete address.

The script works halfway, because it gives me the maximum value in the KmFin column, but what doesn't work is the filtering through the IdCoches field.

Javier C. Martinez Prieto
0 Kudos
AnnelieseMyers
New Contributor

Did you end up finding a solution? I'm also looking for a way to do this. 

0 Kudos
JavierCMartínezPrieto
Occasional Contributor

Hi @AnnelieseMyers,


unfortunately I have not found the solution to this case, the alternative I used was the use of the search function, the only problem is that it does not bring the value itself, but it shows it for you to select.

Currently the pulldata(@layer) function has been added, with this new utility you can bring values from the service but I have not been able to test it yet.

Greetings Javier

good
unfortunately I have not found the solution to this case, the alternative I used was the use of the search function, the only problem is that it does not bring the value itself, but it shows it for you to select.

Currently the pulldata(@layer) function has been added, with this new utility you can bring values from the service but I have not been able to test it yet.

Greetings Javier

Javier C. Martinez Prieto