Advanced Calculations for Parent/ Child cases



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