scripts:register

Form register

Scripts also allow you to use the form register in your calculations.

Imagine a report form that has three fields: Year, Number and Document (which is used to attach the report file). The script below allows you to do a duplicate check. It accesses data from the form register and Pyrus lets you know if the report has been released before.

form.onChange(['Year', 'Report No.'])
  .validateAsync('Report No.', async state => {
    const [year, num] = state.changes;

    if (!year || !num)
      return null;

      if (year.text && num.text) {
        const duplicates = await form.fetchSelfRegister(f => f
          .fieldEquals('Year', year)
          .fieldEquals('Report No.', num), []
        );

        if (!duplicates || !duplicates.tasks)
          return null;

        const firstDuplicate = duplicates.tasks[0];
        if (firstDuplicate)
          return {
            errorMessage: `Report has already been released`
          };
      }

      return null;
    });
  

If we now try to fill out the form with the data from an existing report, we’ll see an error message and a link to the existing report.

Was this article helpful?

Yes, thanks! No, I have a question