'Trigger' a calculation at the 'end' of a form

You may be interested in this thread, since its very closely related: Spec proposal: end-geopoint to automatically capture location upon form completion

My thought was that I could use the metadata field ${end} as a trigger. However, that didn't work.

Correct. XLSForm 'triggers' are in actuality xform-value-changed events, whose action is to perform a setvalue operation; see https://getodk.github.io/xforms-spec/#events for the gory details... :slight_smile: This doesn't work for metadata - like ${end} [which in actuality is an XForm timeEnd] because metadata aren't represented by UI controls, so they dont generate an xform-value-changed event when their value happens to be saved.

So, as described previously, what is really required is adding a new event type to the ODK spec - eg odk-instance-finalize - which could then be used to trigger a setvalue (or setgeopoint) action accordingly, eg when the form is finally finalized and no longer editable.

Until then, the best you can do is perhaps have a required question right at the end of your form, eg an XLSForm acknowldege question (https://xlsform.org/en/#question-types) against which you put a trigger to save some calculation result. But, as you've already pointed out, this still doesnt prevent the user from answering this but then going back and changing previous answers, so its far from an ideal solution.

AFAICT there's no foolproof way yet - without an actual odk-instance-finalize (or equivalent) new event type - to accomplish this.

Note, calculations are being re-calculated when you finalize your form; from https://docs.getodk.org/form-logic/#when-expressions-are-evaluated :

When expressions are evaluated

Every expression is constantly re-evaluated as an enumerator progresses through a form. This is an important mental model to have and can explain sometimes unexpected behavior. More specifically, expressions are re-evaluated when:

  • a form is opened
  • the value of any question in the form changes
  • a repeat group is added or deleted
  • a form is saved or finalized

Although, again, when the form is finalized may not necessarily match what you mean by the 'end' of a form [sic]

2 Likes