Diagnosing indexed-repeat() errors

You're testing your form and suddenly an error message pops up and suggests that you use the indexed-repeat() function:

Error evaluating field 'x': error in evaluation of xpath function [y] => org.javarosa.xpath.XPathTypeMismatch Exception [ This field is repeated: ... You may need to use the indexed-repeat() function to specify which value you want.

Why are you getting this error

The error message means that you're trying to refer to a repeated field – perhaps in a constraint or relevance expression – from outside the same repeat group, but without first enclosing the reference to the repeated field inside the indexed-repeat() function. If you refer to a repeated field with the simple ${fieldname} syntax from within the same repeat group, then SurveyCTO figures that you want the response to the "fieldname" that corresponds to the current repeat-group instance; for example, if you refer to ${age} inside a household roster group, then SurveyCTO figures you want the age for the current household member. But if you refer to ${age} outside the repeat group, then there is no current household member... and so SurveyCTO can't know which age you want.

That's where the indexed-repeat() function comes in: it allows you to specify exactly which instance or copy of a repeated field you want. For example, instead of a relevance expression like "${age} > 25", I could use the following to check the age of the first household member:

indexed-repeat(${age}, ${repeatgroupname}, 1) > 25

If you are unfamiliar with the indexed-repeat() function and want a detailed explanation of its syntax, please read this help topic. Below, I have assumed that you have already read that help topic.

Understanding the error

The error message actually gives some clear clues that will help you find where in your form you need to use the indexed-repeat() function. The second section of the error message shows the full path of the repeated field that is causing the error. To take the error message in the above image as an example:

/consumption_test_units_v4_count/consented[1]/unit1[1]

This means there is a field in this specific survey, called "unit1", which is inside a group called consented. This field is inside a repeat group, hence there is more than one copy of this field (there is one copy for each instance of the repeat group). However, elsewhere in the form, outside the repeat group, you are trying to refer back to this this ${unit1} field without using indexed-repeat().

The solution

To resolve the error:

  1. Note the name of the repeated field from the error message.
  2. Open your form (either in Excel or in the online form designer).
  3. Search for the field name referenced in the error message (using CTRL-F in Excel or the Search button at the top of the online form designer), and inspect each reference to the field.
  4. If you find any references to the field that are outside its own repeat group –perhaps in a calculate, constraint, or relevance expression – make sure the field is enclosed inside indexed-repeat() rather than being referenced directly. If the ${fieldname} reference is directly in a label or hint, you will need to add a calculate field to call indexed-repeat(), then reference that calculate field's value in the label or hint.

Once all of your repeated-field references are either (a) inside the same repeat-group context (so that there's a current instance) or (b) inside indexed-repeat() calls, you can update and re-test your form. The error should be resolved.

More help

If you are unable to resolve the error after following the above guide and you are a supported SurveyCTO user, you can submit a support request and someone from our team can help you investigate further. Click on My requests in the top right of this page to do so if you are a user on a paid server with access to support. Please just make sure to attach an Excel copy of your form. If you're a community user and don't have professional support, you can post the issue, along with your Excel form, to the community forum for assistance from other community members.


Functions and operators referenced in this article: indexed-repeat(). For a full list of SurveyCTO functions and operators, please read this help topic.

 

0 Comments

Article is closed for comments.