This page will walk through an example of setting up a form that both displays conditional options in lists (list of districts from chosen state) and has options that are specific to each user.
First setup the following two tables using the Creating and Updating Lookup Tables page. You can use whichever mobile workers or groups that you have in your project.
|field: id||field: name||user 1||group 1|
|up||Uttar Pradesh||supervisor||Uttar Pradesh Users|
|field: id||field: name||field: state_id||user 1||group 1|
|agra||Agra||up||supervisor||Uttar Pradesh Users|
|faizabad||Faizabad||up||supervisor||Uttar Pradesh Users|
Our sample creates two tables (state and district). The district table has three fields (id, name and state_id). The state_id will be used to filter the list of districts based on the selected state. The following image shows our final setup.
Create a Multiple Choice/Checkbox Question Based on a Lookup Table
NOTE: Lookup tables referenced in Multiple Choice or Checkbox questions should not have values with spaces in them. This can cause data issues in your app. Please define the values in the lookup tables without spaces.
To create a multiple choice or checkbox question based on a lookup table, first add the question to your form.
In our example, we'll add two multiple choice lookup table questions, "state" and "district".
Now go to "Lookup Table Data" and select the correct lookup table and value and label fields. When inputting the value and labels an autocomplete widget will show you valid options for these fields.
If you were to test this application, you'd see the first question displays a list of all states and the second question displays a list of all districts (irrespective of the selected state). To filter the district list, we need to add a filter condition to the choices.
Filtering the Choices
To filter the set of choices, you can use the 'Filter Option' under 'Lookup Table Data' (similar to the expressions used when defining Display Logic or Calculations in your form). In our example, we'll filter the District item list based on the selected state. Our district table contains a field called "state_id". The values in this match the value specified for the state question.
The left option will show an autocomplete widget to help you pick a lookup table attribute and the right option will allow you to drag a question from the question tree.
Loading a lookup table value into a hidden value instead of a multiple choice/checkbox question
Sometimes you'll want to pull information from a lookup table into a hidden value. For example, from the example above, you want to save another case property called "project_name" and after the user selects a district, you already know which project it falls under. You create a hidden value question under the district question and name it "project_name". Then you create the following lookup table:
|id||name||project_name||user 1||group 1|
|agra||Agra||agriculture||supervisor||Uttar Pradesh Users|
|faizabad||Faizabad||agriculture||supervisor||Uttar Pradesh Users|
In the hidden value you'll enter the following calculation: instance('item-list:project')/project_list/project[id = /data/district ]/project_name. Depending on what the user chooses for district, the value for "project_name" will be either 'nutrition' or 'agriculture'. From the formula, the first 3 'project' refers to the lookup table name. 'id' is the value from the column in the lookup table that is matched with the question 'district', which has '/data/district' path in the form. The last part of the formula is 'project_name' and will be the value taken from the same named lookup table column and loaded into the hidden value.
How to enter your own value using an 'other' option
There might be times where you want to give the option to enter a location or value that might not be in the lookup table. You can do so by adding an 'other' option to your district table and a text question to your form that will only be displayed when the 'other' option is selected. The following steps will show you how to do so.
Using the example above, we want to allow the user to enter a district that is not in the lookup table. To do so, you can add an ‘other’ field to the district lookup table:
Uttar Pradesh Users
Uttar Pradesh Users
You then proceed to create the form like you did above:
Once the locations are set up, you need to add another filter to the lookup table question in the form. This filter will be based on the district ID, so the first field should be set to ‘id’. The second field should be set to ‘is equal’. And the third field should be set to ‘other’. We also want this option to occur only when ‘other’ is selected, so we also need to change the ‘Result is’ field to be ‘True when ANY of the expressions are true.” Be sure to save the expression once you are done!
After saving the new expression and returning to the question properties page, you should see the full expression:
Now you just need to add another text question to the form that will be displayed only when ‘other’ is selected:
Now when your users select ‘other' for the district, they will be able to proceed to another question that will allow them to enter the district name.
Testing an application with lookup tables
In order to test the filtered choices and other logic works in the application, you must sign on as a mobile user and submit 'real' data to the server. If you see an error when opening the form that states "Could not find an appropriate fixture for src: jr://fixture/item-list:table_name" you have not properly setup your lookup tables.
- Sync your application. Some users have reported this error with large lookup tables that have not properly downloaded which choosing "Update Commcare" from the menu. Run the sync to ensure all assets are properly downloaded to the device
- Make sure that the current user has lookup table rows assigned to them (Creating and Updating Lookup Tables)
- Alternatively, setup your lookup table so that it is available for all users (For the lookup table, set it up so that it is "Visible to All Users" by clicking Edit Type).
- Once you've done this, make sure you've synced your phone.
If you run into issues testing your app with lookup tables, try to Clear user data. After you sync your phone if the lookup table still is not working, you may need to clear user data and log-in again. To do this, go to the login screen and click on Settings. After the Settings screen shows up, click the menu button, and select the first option, "Clear User Data." This will clear the username and force you to re-enter login credentials. Once you log in again, the new or edited table should be accessible to your user.
Training an application with lookup tables
- Generic User: Create a generic user called "dimagi" or "demo" and update the table specifically for this user - populating all the conditional select options with the same 'dimagi' or 'demo'. As long as the partner is aware of the use case, they will be able to filter out this user in reports. During training, mobile users will sign onto CommCare and submit live 'practice' data. Work with the partner to specify a date where all data submitted to CommCare HQ is considered actual data for program purposes. This should be made clear to the FLWs and to the data analyst/M&E officer.
- Training Application: Copy the final application - remove the conditional select options from the application. This app can be used during training as a training application and the users can enter data in demo mode.
- No SIM: If the partner has not secured SIM cards for the phones prior to training, install CommCare on all the phones. Use one SIM card to restore the user or practice user ('dimagi' or 'demo') to all phones. The phone will update according to the latest lookup table on HQ. Remove the SIM. The conditional select options will work on the phones and the data will not be submitted to the CommCare HQ.