Hi everyone,
I'm working on a Survey123 form where I need to calculate the next due date for a semiannual payment (always due on June 30 or December 31). I’m using a JavaScript function via pulldata("@javascript") to do this, and it works perfectly in Survey123 Connect. However, in the web version, the field stays empty—even when I manually trigger recalculation.
Here’s what I’ve done:
The field that should populate with the output has the following calculation: pulldata("@javascript", "vencCanon.js", "calcularProximoVencimiento", max(${fecha_venc_canon}), ${fecha_inicio_canon})
Here’s the JavaScript I'm using (vencCanon.js):
function calcularProximoVencimiento(ultima_fecha_venc, fecha_inicio_canon) {
let base;
if (ultima_fecha_venc) {
base = new Date(ultima_fecha_venc);
} else if (fecha_inicio_canon) {
base = new Date(fecha_inicio_canon);
} else {
return null;
}
let dia = base.getDate();
let mes = base.getMonth(); // 0-indexed
let anio = base.getFullYear();
let proximo;
if (mes === 5 && dia === 30) {
proximo = new Date(anio, 11, 31);
} else if (mes === 11 && dia === 31) {
proximo = new Date(anio + 1, 5, 30);
} else {
if (mes < 6) {
proximo = new Date(anio, 5, 30);
} else {
proximo = new Date(anio, 11, 31);
}
}
// Ajustar a las 3:00am para evitar desfasaje por zona horaria
proximo.setHours(3, 0, 0, 0);
// Formato completo con hora
return proximo.toISOString();
}