I want all pregnancy_pp cases whose grandparent case_id is the same as the family_id

commcare

We are trying to get a warning label to display if someone is registering a pregnant client in a family that already contains a pregnant woman. Clients are child cases of family cases. Pregnancy cases are extension cases of clients.

Best Answer

The expression is:

count(instance('casedb')/casedb/case[selected(join(" ", instance('casedb')/casedb/case[index/parent = instance('commcaresession')/session/data/case_id_load_family_0]/@case_id), index/host)][@status = "open"][@case_type = "pregnancy_pp"]) > 0 and /data/pregnancy_pp/pregnant = 'yes'

To explain:

instance('casedb')/casedb/case[index/parent = instance('commcaresession')/session/data/case_id_load_family_0]/@case_id

This part gives the case_ids of the clients in the current family However, it is not structured like a space-separated list. It's structured XML. So we couldn't use selected() on that. So we first converted the structured XML to a space separated list with join(" ", xxxx) Then we were able to stick that into the selected(xxx, index/host).