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