Child Cases
Overview
Child cases (also known as subcases) let you open other types of cases for use in other modules. They will be linked to the parent case so you will always know where they came from. The classic use case of a child case is a mother giving birth to a newborn that has to be tracked separately.
Child cases can be configured on the "case management" tab of any form that uses case management. Child cases can be opened from any question in a form including from a repeat group.
To enable this feature, proceed to the Application Settings by selecting the gear in the top left hand corner next to the application name. Then select the Add-On tab. Once on the Add-On tab, select the menu option on the right (three stacked lines) and check the Case Management box. Child cases will appear as an option in this section.
Example Use of Child Cases
Let’s assume you’re working with a Maternal Child Health (MCH) application in which you’re monitoring two different types of cases – pregnant/nursing mothers and children. Using child cases allows us to bypass having multiple registration forms for the different types of cases. For example, you can open a child case from a mother's birth form instead of having to fill out a separate child registration form.
On CommCare HQ, go into your desired application. Click on the form in which you are registering your subcase/ related case.
Select the “case management” tab. On the bottom of the case management tab, you will see the heading “Child Cases””. Select the plus (+) sign to open the secondary case you want that is linked to the “main” case. For example, if the child case were a baby, select the module in which the case type is "baby."
If desired, you can add a question that serves as the condition to open the child case (i.e. Only if the answer to <question> is <answer>).
Select the questions/data points that you want to also be associated to the child case. At a minimum you must specify a question that serves as the name of the new case.
Save your changes and try it out!
Opening Child Cases from a Repeat Groups
It is possible to open a child case from questions that are in a Repeat Groups . This is useful if you want to be able to open multiple child cases of one type - for example if you want to be able to conduct a survey and register a case for every household member.
This is set up just like a standard child case block on the case management settings. However, keep in mind the following:
You will not be able to use the questions in the repeat group to update the parent case, only to update the child case.
The "name" property must come from the repeat group, not from the parent case.
If you have multiple repeat groups that create cases in a single form, all properties saved to a single child case must be from the same repeat group. That is, you can't mix questions from different repeat groups when creating child cases.
Accessing Parent Case Properties
The application builder gives Child cases the ability to access and update properties of the parent case.
More concretely, this means you can update the parent case's properties from a child case's form, and load parent properties into a child's form.
To reference a parent property, type:
parent/property_name
For example, on the case configuration tab for one of the forms in the child module, to refer to the Parent case property "dob", you would type
parent/dob
into the case property box.
Note that this only works one-way: the child can access/update its parent case's properties, but the parent case CANNOT access or update properties of its child cases. This one-way relationship can be seen when syncing cases to a device as well. A child case synced to a device will bring the parent with it. A parent case synced to a device does not guarantee that the child cases will also be there.
See Case Data Export | Linking Parent and Child Case Data.
[Advanced] Linking Parent and Child Case Data
Warning
These calculations are advanced, and require complex expressions.
Retrieve the Case ID of the Parent of the selected case
In a form where a case was selected, the id of that case's parent (if one exists) can be referenced as
instance('casedb')/casedb/case[@case_id = instance('commcaresession')/session/data/case_id]/index/parent
Count of all Child Cases
You may want to get a count of all the open child cases that a specific parent case has. The expression you should use is:
In a form:
count(instance('casedb')/casedb/case[index/parent = instance('commcaresession')/session/data/case_id][@status = 'open'])
In a form display condition:
count(instance('casedb')/casedb/case[index/parent = @case_id][@status = 'open'])
Displaying Only Parents with Child Cases
In a case list, if you'd like to filter the case list to only show parents that have open child cases, use the following expression:
count(instance('casedb')/casedb/case[index/parent = @case_id][@status = 'open']) >= 1
Note: both of these expressions will only work if the both parent and all child cases are owned by the mobile worker that is in use.
Displaying Parent Case Property
In a case list or case detail, use the following expression as a calculated expression for Display Properties:
instance('casedb')/casedb/case[@case_id = current()/index/parent]/<case_property_name>
For example, if the <case_property_name>
is unit_description
the expression will be:
instance('casedb')/casedb/case[@case_id = current()/index/parent]/unit_description