Calculation error (loop)

I've read resolutions to such scenarios on the forum to no avail, kindly assist:

Error in calculation for ..... attempting to select elements 0 of a list with only 0 elements

Can you provide some additional context for the issue, like the calculation that's failing?

Hey Clayton,
Thanks for your patience.
I have a multi-select question that pulls its values from a lookup table. Once the user has selected some of these options, I want to show them to the user in a comma-separated list.
This error occurs after selecting multiple items from the lookup table. The loop is to display the selections for every training attendant. I'm having issues displaying multiple selections.
Thanks.

Are you using the join() function to create the comma separated list? Can you share the expression itself?

`Yes I am,
join("--", #form/loop_topic_agr/topic_agr_name)

Hm, that expression looks correct and shouldn't cause any issues.

Are you doing a

selected()

call somewhere? It looks like it may be related to a separate loop, actually (related to the 'od' loop)

Here is the main expression, I doubt there might be an error somewhere, maybe!?

instance('TZ_Tables_TrainingTopics')/TZ_Tables_TrainingTopics_list/TZ_Tables_TrainingTopics[topic_id = selected-at(#form/topic_agronomy, position(..))]/topic_name

Hi Ed,

This is probably a timing or ordering issue where #form/topic_agronomy isn't yet populated during one of the evaluation cycles.

The easiest change is probably just to wrap the full expression with an if to ensure that the indexed element exists first, like

if(
count-selected(#form/topic_agronomy) > position(..),
instance('TZ_Tables_TrainingTopics')/TZ_Tables_TrainingTopics_list/TZ_Tables_TrainingTopics[topic_id = selected-at(#form/topic_agronomy, position(..))]/topic_name,

"")

Hey Clayton,
Thanks, unfortunately, the syntax only displays the concatenation of only the first selected topic in the multiple of the selection (if I selected 2/3 topics it displays the first topic 2/3 times).

Hm, that sounds like a secondary issue with the calculation outside of this fix.

I'm guessing it's likely due to the position() function, which might be referencing the wrong node? That would result in it always returning '1' and thus always selecting the first item.

Exactly, that's it. I have tried various selection methods and what you described seems to be the case, it isn't displaying the 2nd and so forth selection in multiple selections.
Below is an example of two selection in a multiple selection:

image

I think the issue is that your position() function is actually being evaluated in the relative context of what ".." means inside of your filter, which I think might actually be this node:

instance('TZ_Tables_TrainingTopics')/TZ_Tables_TrainingTopics_list

If you want repeat specific context within a filter you generally need to use the "current()" function to recover the reference to the in-form node.

Can you try replacing position(..) with position(current()/..) ?

1 Like

Thank you so much Clayton, seems to be working fine.

whew, glad to hear it!

1 Like

Hi Clayton,
I'm experiencing the same error in another application regardless of trying the method we tried before, look below:

Calculation Error: Error in calculation for /data/subject_topic/loop_topic_youth/topic_youth_id Attempting to select element 1 of a list with only 1 elements.

This could be the result of a couple of different things. It could be that the position() function is providing a 1 indexed output into a 0 indexed list, for instance.

As a first pass, have you confirmed from splitting out the function calls what the position() function returns in this context, and what the list it's filtering looks like? I'd start by creating a calculation which spits out the position() call result as well as another calculation which does, say, a join('|',....) of the values being indexed to confirm that the list is actually what's expected.

I have the same error but only showed on webapps. On the app preview, I can proceed with the process. On the android app, it made the phone hung.

Here's my error message: Calculation Error: Error in calculation for /data/pile_pallet_group/pallet_repeat_group/pallet_case_id Attempting to select element 6 of a list with only 6 elements.

the pallet_case_id calculation: join(" ", instance('casedb')/casedb/case[@case_type = 'pallet' and shipment = #form/pile_shipment_list/shipment_id_1]/@case_id)

and I don't have any problem using the same case data on the test environment.

can anyone explain this?