Hi everyone,
I have a JavaScript function that calculates date fields.
In my form i have the following configurations to make it work:
calculation: pulldata("@javascript", "calcularFechaVenc.js", "calcularFechaVenc", ${fecha_not_insc_concesion},700, ${periodo_de_trabajo})
relevant: string-length(${fecha_not_insc_concesion}) > 0 and (${periodo_de_trabajo} = 'plena_temp' or ${periodo_de_trabajo} = 'semi_temp' or ${periodo_de_trabajo} = 'todo_el_anio')
The thing is, when i test this in the Survey123 Connect app. The target fields populate correctly, but when i do the exact same workflow in the browser, the fields doesnt fill. No error appears in console.
I'm using Connect 3.22
Any advice would really help me. Fell free to ask for more info
Solved! Go to Solution.
function calcularFechaVenc(fechaInicio, cantidadDias, periodo) {
let fecha = new Date(fechaInicio);
let diasContados = 0;
while (diasContados < cantidadDias) {
let mes = fecha.getMonth();
let esValido = true;
if (periodo === 'plena_temp' && mes >= 5 && mes <= 9) {
esValido = false;
} else if (periodo === 'semi_temp' && (mes === 6 || mes === 7)) {
esValido = false;
}
if (esValido) {
diasContados++;
}
if (diasContados < cantidadDias) {
fecha.setDate(fecha.getDate() + 1);
}
}
return fecha.toISOString().split("T")[0];
}
This is how I’m calculating the dates. I’ve already explored all other possibilities — this workflow can only be achieved using JavaScript. Since the target fields are set to read-only, they remain invisible until the relevant condition is met. When that condition becomes true, the fields appear, but they show up blank (displaying DD-MM-YYYY, --:--).
If the issue is that the JavaScript function only triggers on load, what would be the recommended solution?
UPDATE: Setting the fields to read-only = no did the trick. The only issue now is that I have to click the “Recalculate” button that appears below the question for the values to populate
A couple thoughts:
Are you logged in when testing on the webapp, Javascript functions don't work for public surveys.
Sometimes JS functions triggers upon load and don't recalculate when new data is added. I see you have a relevancy but have you tried calculationMode = always on that field.
How are you calculating the dates, is it possible to do outside a JS functions?
function calcularFechaVenc(fechaInicio, cantidadDias, periodo) {
let fecha = new Date(fechaInicio);
let diasContados = 0;
while (diasContados < cantidadDias) {
let mes = fecha.getMonth();
let esValido = true;
if (periodo === 'plena_temp' && mes >= 5 && mes <= 9) {
esValido = false;
} else if (periodo === 'semi_temp' && (mes === 6 || mes === 7)) {
esValido = false;
}
if (esValido) {
diasContados++;
}
if (diasContados < cantidadDias) {
fecha.setDate(fecha.getDate() + 1);
}
}
return fecha.toISOString().split("T")[0];
}
This is how I’m calculating the dates. I’ve already explored all other possibilities — this workflow can only be achieved using JavaScript. Since the target fields are set to read-only, they remain invisible until the relevant condition is met. When that condition becomes true, the fields appear, but they show up blank (displaying DD-MM-YYYY, --:--).
If the issue is that the JavaScript function only triggers on load, what would be the recommended solution?
UPDATE: Setting the fields to read-only = no did the trick. The only issue now is that I have to click the “Recalculate” button that appears below the question for the values to populate
Did you try "calculationMode = always" in the bind::esri:parameters column?
I tried, but as long as read-only is set to yes, the problem persists. To resolve the recalculation issue, I had to remove the relevant expression. Now I need to find a way to keep that field protected while still allowing the recalculation to work.
Thanks for the help!