Tricky multiple choice question

Hi guys

Now, I'm calling this "tricky" because i haven't figured it out yet and was hoping some guru would be able to help me out.

First: The scenario ->
A: I have a registration form for a case type "Family"
B: inside the registration form for Family, i have a repeat group with 3 questions to record family members for example. let's assume i'm asking for name, age, and gender. These are also used to create child cases of the type "Individual" or "family_member"
C: after the repeat is finished, a few questions later, i ask a question "Which family member is the head of household" for example
D: I would like to see the list of family members in a single-select multiple choice.

I am well aware that we can use the child cases as a multiple-choice lookup table. but, for that to work, i'd need this form to be closed before the parent and child cases are created and then can appear in a lookup

So, the easy fix is to close the registration form and do a second followup form. But, i have a feeling the some of the users are going to turn around and ask if we could have it in the same form without having to navigate somewhere else.

Any ideas? I thought of reconstructing whatever format the multiple-choice lookup likes, but i found out that it likes XML and it seems a little overkill to construct an XML string to feed it when i don't even know if it would work.

Any ideas?

You should be able to use a similar strategy to what you describing (using the case db) but instead of querying the Case DB you query the form.

Relevant wiki docs: Multiple Choice Questions with Cases as Choices - CommCare Public - CommCare Public

  • Choose "Lookup Tables -> Multiple Choice Lookup Table" from the Add Question control
  • Choose the (...) button and enter the following:
    • Set the Query Expression to /data/path/to/repeat
    • leave the Instance ID and Instance URI blank
    • click save
  • Set the Value Field and Display Text Field to the names of the questions in the repeat group

Simon, your solution works great! you are my hero! Somehow, i didn't realize that i could just query the form!

If i could mention this, i was looking at valid xPath expressions i could use in calculations. Would you be able to point me towards them? At some point i was trying to concatenate all the entries in the repeat group and show them as a label (just to see what it would look like) and i got an error saying that it doesn't work, i tried concat and string-join. Any idea where i can find out what works?

Glad that worked.

This page has some info on repeat group logic: Advanced Functions for Repeat Groups - CommCare Public - CommCare Public

The function documentation also has some examples. For instance the documentation for join shows how to join data from repeats:

join(", ", /data/my_repeat/child_name)

1 Like

@Mazz,

Jumping on this thread. I am using the information in this documentation to try to pull the child cases created but the names do not show up

My farmer names do not show up. My scenario is: Create a new group and register farmers under it using a repeat function. I then set up another menu (to register clusters) using the same case type as the one used for creating the groups.

The cluster form is set up as a follow up form so when using it you first have to select the group then select the farmers. One group can have multiple clusters, so i want to be able to see farmers in the selected group.

This is the function i am using for my lookup table data:

instance('casedb')/casedb/case[@case_type='bwc_3_ng_group'][index/parent = instance('commcaresession')/session/data/case_id]

It doesn't display errors but the farmer names do no show up

I don't see any particular issues with your casedb expression, so I think the issue is something more subtle.

My advice is to use the Web Apps Data Preview feature to evaluate the expression here in pieces and make sure that each piece is returning what you expect, ie:

instance(‘casedb’)/casedb/case[@case_type=‘bwc_3_ng_group’]

instance(‘casedb’)/casedb/case[index/parent = instance(‘commcaresession’)/session/data/case_id]

instance(‘commcaresession’)/session/data/case_id

If the full expression itself is returning the expected set of cases, it might have something strange to do with how the lookup table question is situated in the form, but my guess is that it's more on the data side.

-Clayton

@Clayton_Sims, thanks for helping. please see the images below as products of testing the syntax piece by piece.

instance(‘casedb’)/casedb/case[@case_type=‘bwc_3_ng_group’]

instance(‘casedb’)/casedb/case[index/parent = instance(‘commcaresession’)/session/data/case_id]

instance(‘commcaresession’)/session/data/case_id

So only the first expression works by actually pulling the group name but what i need are the names of the farmers registered under the particular group.

@Clayton_Sims, this is what it looks like when i try to reference the child cases directly from the lookup table

I think I might have needed to provide more context for my suggestion. The web apps data preview feature is something that doesn't require you to change your forms at all. You can use it while you are looking at a form in the Web Apps or Live Preview system to run queries against the "live" form and casedb structure to see what they are evaluating to in practice.

You can access the data preview by clicking the Blue Bubble on the Live Preview frame and you can do the xpath eval test by choosing it from the "three line" menu from within Data Preview.

And the error you see above like

Parse error in XPath path

Comes from using fancy quotes () instead of simple ones ('). This happens often when copy/pasting from somewhere that doesn't treat it as code.

This will fail:

instance(‘commcaresession’)/session/data/case_id

This won't:

instance('commcaresession')/session/data/case_id

Hey Jay

3 points:
A: Yes, I agree with Ethan that does look like a fancy quote.
B: You don't always need to use all 3 conditions that you've set up to get the child cases. But i think that if you get rid of the fancy quotes you'll get some results back.
C: Make sure you're putting the right values in the value field. if you use a field that has blanks or spaces or special characters this will throw an error when you try to load those values in to the multi-select. so use something like caseid or something that you are 100% is database compatible

Good luck!

Mazz

@Ethan_Soergel, @Clayton_Sims

Thanks everyone, the fancy quotes were the problem.