Display count of child cases as a calculated property in the case list

Hi Ralph,

The rabbit hole can get kind of deep on this one. I'm going to write up a pretty complex way to achieve this below, but I won't be able to test it live.

If you want to achieve something like this I strongly suggest that you read up on using the Web Apps Data Preview system's ability to test XML expressions live, and test the expression inside of a form (replacing the 'current()/@case_id' expression with the appropriate expression inside of a form) before applying to your app. You can break down the expression into pieces to make sure they are outputting what you expect to test them.

Counting Grandchildren

The logic currently being expressed is "Count the set of cases who have a parent index with the ID "current()/@case_id"

instance(‘casedb’)/casedb/case[index/parent = current()/@case_id]

You can break down the new request in a more complex manner: Take the set of cases who have a parent index with the ID "current()/@case_id" (what we are counting here), and count the set of cases who have a parent index which is in that set.

We can create a set of child case IDs with this subexpression

join(' ', instance(‘casedb’)/casedb/case[index/parent = current()/@case_id]/@case_id )

which will give a space-separated list of those case ID's. You can use the selected() function to then see whether another case's parent index happens to be included in that set, and then count that list

count(instance(‘casedb’)/casedb/case[selected(LIST_OF_CHILDREN, index/parent) ])

resulting in the final expression

count(instance(‘casedb’)/casedb/case[selected(join(' ', instance(‘casedb’)/casedb/case[index/parent = current()/@case_id]/@case_id), index/parent) ])

Quick note: I expect this will be fast due to parts of the xpath query engine that pre-analyse and speed up these kinds of expressions, but those optimizations can fail if expressions don't meet an expected pattern. I'd also strongly recommend testing the performance of these queries at full load before deploying them in a live app.

-Clayton

1 Like