Select to view content in your preferred language

Survey123 JavaScript calculation returns empty date field in web form but works in Connect

113
4
Monday
JuanTomásMartinez17
Esri Contributor

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:

  • I have a date field called fecha_prox_venc whose value is calculated using the script below.

  • The function takes two inputs:

    • fecha_venc_canon: inside a repeat, refers to the last due date payed

    • fecha_inicio_canon: the initial date to start counting from, if no payment has been made

 

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();
}

 

0 Kudos
4 Replies
DougBrowning
MVP Esteemed Contributor

This post is from 2020 but it does say at the bottom of the post.

Passing repeats or questions within a repeat to a JS function only works in Connect and the mobile app

https://community.esri.com/t5/arcgis-survey123-blog/extending-survey123-smart-forms-with-custom-js/b... 

That could be it.

TylerGraham2
Frequent Contributor

The JavaScript functions in survey forms Repeat section appears to support what Doug is saying.  

A possible solution to explore is if you have access to ArcGIS for Power Automate or some other workflow automation tool you might be able to set it up to fill the due date in that field when a new survey is submitted.  

Neal_t_k
Frequent Contributor

If I am reading you JS script correctly you could set up that logic directly in the form without the JS.  You would end up with a pretty complex if statement though.

0 Kudos
Neal_t_k
Frequent Contributor

@JuanTomásMartinez17 Try the attached out, should get you close to adjust as needed.

0 Kudos