Help with JavaScript Script for Survey123 | Ayuda con Script de JavaScript para Survey123

377
1
Jump to solution
12-13-2021 11:47 PM
JavierCMartínezPrieto
Occasional Contributor

Help with JavaScript Script for Survey123
Good morning, everyone.
I need help with a JavaScript script for Survey123.

This is the script:

function getLastGeneratorReading(Parte_de_vehiculo,token){
    var featureLayer = "https:\\services3.arcgis.com\dha0QtUTrPxlyZDZ\arcgis\rest\services\service_fcedea123ecd4de38c63d01eae3f53c0\FeatureServer\0";
    var xmlhttp = new XMLHttpRequest();
    var params = "/query?";
    params = params + "where=Parte_de_vehiculo=+" + Parte_de_vehiculo + "'&outFields=KmFin"
    params = params + "&returnGeometry=false&orderByFields=IdCoches DESC&resultRecordCount=1&f=json";
    if (token ){
    params = params + "&token=" + token ;
    }
    var url = featureLayer + params;
    xmlhttp.open("GET",url,false);
    xmlhttp.send();   /*peticion asincornica*/
    if (xmlhttp.status!==200){ /*peticion  sincronica*/ /*provoca error debido: porque te bloquearía el hilo dejando el cliente bloqueado hasta que se resolviese la petición*/
    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.KmFin);
    }
    else{
    return "";
    }
    }
    }
    }
 

currently it does not crash inside survey123 but it does not perform the function it is designed for.
the operation is basic, it should take the value of the last record that has been entered by a survey in survey123 in a row of a given field.
the value it currently gives is 0. This value is incorrect as it should give me as a result the last value that was sent by the survey.
Thanks in advance to all of you who participate.

Greetings Javier

Update:

value 0 is obtained return (xmlhttp.status); from what I have read it is a problem that it is not able to connect to the web page. would anyone know how to fix this error?

##########En Español#####################################################################

Buenas a todos.
Necesito ayuda con un script de JavaScript para Survey123.
Este es el Script:

function getLastGeneratorReading(Parte_de_vehiculo,token){
    var featureLayer = "https:\\services3.arcgis.com\dha0QtUTrPxlyZDZ\arcgis\rest\services\service_fcedea123ecd4de38c63d01eae3f53c0\FeatureServer\0";
    var xmlhttp = new XMLHttpRequest();
    var params = "/query?";
    params = params + "where=Parte_de_vehiculo=+" + Parte_de_vehiculo + "'&outFields=KmFin"
    params = params + "&returnGeometry=false&orderByFields=IdCoches DESC&resultRecordCount=1&f=json";
    if (token ){
    params = params + "&token=" + token ;
    }
    var url = featureLayer + params;
    xmlhttp.open("GET",url,false);
    xmlhttp.send();   /*peticion asincornica*/
    if (xmlhttp.status!==200){ /*peticion  sincronica*/ /*provoca error debido: porque te bloquearía el hilo dejando el cliente bloqueado hasta que se resolviese la petición*/
    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.KmFin);
    }
    else{
    return "";
    }
    }
    }
    }
   


actualmente no da fallo dentro de survey123 pero no realiza la función para la que se ha diseñado.
el funcionamiento es básico, debería tomar el valor del ultimo registro que se ha introducido mediante una encuesta en survey123 en una fila de un campo determinado.
el valor que da actualmente es 0. Este valor es incorrecto ya que me debería dar como resultado el ultimo valor que se envió por la encuesta.
Gracias de antemano a todos lo que participéis.

Saludos Javier

 

Actualización:

el valor 0 se obtiene return (xmlhttp.status); por lo que he leído es un problema de que no es capaz de conectar con la pagina web. ¿alguien sabría como solucionar este error?

Javier C. Martinez Prieto
0 Kudos
1 Solution

Accepted Solutions
JavierCMartínezPrieto
Occasional Contributor

As this script gave a lot of problems in the end I have used the search option to obtain the result I am looking for.
For search to work you have to create an intermediate table that only has the results we are looking for, in my case I have done it through map viewer classic.

Greetings

 

####################Español##################################################

Como este script daba mucho problemas al final he usado la opción de search para obtener el resultado que busco.
Para que funcione search hay que crear una tabla intermedia que solo tenga los resultados que buscamos, en mi caso la he realizado a través de map viewer classic.

Saludos

Javier C. Martinez Prieto

View solution in original post

0 Kudos
1 Reply
JavierCMartínezPrieto
Occasional Contributor

As this script gave a lot of problems in the end I have used the search option to obtain the result I am looking for.
For search to work you have to create an intermediate table that only has the results we are looking for, in my case I have done it through map viewer classic.

Greetings

 

####################Español##################################################

Como este script daba mucho problemas al final he usado la opción de search para obtener el resultado que busco.
Para que funcione search hay que crear una tabla intermedia que solo tenga los resultados que buscamos, en mi caso la he realizado a través de map viewer classic.

Saludos

Javier C. Martinez Prieto
0 Kudos