Referencing Organizations with filtered multiple choice questions

I’ve set up my project space with Organizations and it has levels 1-4. I would like to set up a series of multiple questions for the users (assigned to levels 2 and 3) to select the facility (level 4) to which the data applies. I’m trying to follow the instructions shown here: https://confluence.dimagi.com/pages/viewpage.action?pageId=38276915 under ‘Making select questions with locations as choices’. I’m getting the error message “Make sure the ‘locations’ lookup table is available, and that its contents are accessible to the current user.” Do I also need to create lookup tables? I thought Organizations eliminated the need for that step? Thank you!

Hi Jenn,

Could you try to Sync with Server on the device that you are getting that error? That error usually means the location hierarchy hasn’t been sent to the phone yet.

-Nick

Thanks Nick. It seems a bad internet connection wasn’t syncing everything without indicating an issue.

Now the organizational structure is loading, but I’m stuck on the filtering. I have a multiple choice with question_id ‘province’ to select the level 1 location, and a multiple choice with question_id ‘district’ to select the level 2 location.

For the second question, when I set the filter to ’ @level-1_id = #form/section_0/province’, it shows everything in the entire hierarchy, not just level-2 locations. I went back into the form builder and it looks like the query expression is being changed when I add the filter expression. So in this second multiple choice question I can have either (a) only level 2 locations but without any filter, or (b) every location in the hierarchy (levels 2-4) that falls under the location chosen for level 1. What I need is a combination of these two things. What am I missing?

Hi Jenn,

You should be able to filter by both of those writing your filter expression as:

@type = ‘level-2’ and @level-1_id = #form/section_0/province

There’s also a button to the right of the “filter” box that lets you add the filters more easily

I’ve got my filters working, thanks all! Now a question about the data itself… because the filters reference @id, that is what’s being recorded in the form data. How can I have it record the name of the location rather than (or in addition to) the id of the location?

xHi Jenn,

If your lookup tables are large enough to cause performance issues with this filtering, you can update the filters to use a different syntax but you’ll need to enable Custom Single and Multi-Select Answers in your form.

After doing so you can define a custom lookup table query referencing the locations structure. The key change will be shifting from defining your filter all at once (X and Y) to defining multiple filters in a row [X = ‘x’][Y=‘y’]

In your case I believe that query and filter would be something like

instance(‘locations’)/locations/location[@type = ‘level-2’][@level-1_id = #form/section_0/province]

I believe you will need to update the #form reference to instead use a full path to that question in the “/data/” syntax, but I could be wrong about that.

The second confluence page also illustrates referencing a location’s name from the locations structure after the ID has been selected, to answer your latest question.

-Clayton

Reviving this thread. I'm trying use Organization levels as part of a lookup table based on the user. I have the levels set as Department > Municipality and it works as expected until I try to filter the municipality based on the department selected. Whenever I add a second filter based on the previous selection it makes the choices blank. I've used all the suggestions in this thread. Can anyone confirm this still works and/or am I missing something new since confluence has the same documentation as this thread? Thanks in advance.

I just made a test form that does this. It has two questions, both "multiple choice lookup table".

Q1: Select a state
Query expression:
instance('locations')/locations/location[@type="state"]

Q2: Select a location in that state
Query expression:
instance('locations')/locations/location[@state_id = #form/q1]

This seems to work fine for me, though you might also want to make the first question required and make the second only visible once the first is filled out.

hi @Ethan_Soergel -

thanks for the swift response. I'm still struggling a bit... :pensive: I've setup Q1 no problem and am running into issues with Q2. In your example where is @state_id defined? Also, should there be two filters in Q2?

Here's what I have setup in the levels.

image

Here's how my Departamento question is setup:

Here's the query for departamento:

Now for the struggle :oncoming_bus: that is Municipio. The following works fine without any filtering. However, it pulls all the municipio regardless or departamento.

When I try to do the following it does not work:

What step did I go wrong? What did I completely miss?

thanks again!

In your example where is @state_id defined?

Each location is given an attribute for each level in the hierarchy, called @LEVELCODE_id, so for me that's @state_id. For you, that should be @departamento_id. That is, in the last screenshot, your filter expression should be

@type = 'municipio' and @departamento_id = #form/departamento_ingreso

There's a little more information on this page, under the "Tiered Location Selection" section:

https://confluence.dimagi.com/pages/viewpage.action?pageId=38276915#ReferencingtheLocation/OrganizationHierarchyinApplications-TieredLocationSelection

I'd also recommend expanding the "Example location fixture structure" below it, as that shows the xml structure this expression operates on. You can view what this looks like on your project using the OTA Restore API

Solved!

Thanks so much @Ethan_Soergel - you were able to help me piece it all together and it looks like I neglected to set the value @id for the first questions. User error indeed!