1

I'm looking to use a display condition for an indicator where the goal is:

If a child case of the parent that I'm editing is open, the indicator should count otherwise it should not count.

In other words, a child "pregnancy" case gets created when a patient is pregnant. If the pregnancy ends, this child case will get closed. Therefore I want to count only the indicator only when the child pregnancy case is open otherwise means the person is not pregnant anymore.

Is this possible in the display condition field?

2 Answers 2

3

Yes, you can do that in the Advanced Mode of the Display Condition.

You will need:

count(instance('casedb')/casedb/case[index/parent = instance('commcaresession')/session/data/case_id][@status = 'open']) > 0

To break this down a little:

  1. The instance('casedb')/casedb/case gets all cases
  2. The [index/parent = instance('commcaresession')/session/data/case_id ] portion filters all the cases down to those that have a parent_id of your current case
  3. The [@status = 'open'] filters for only opened cases
  4. We have all cases who are children of the current case and that are open. That will return a list of cases. The count function will count how many there are.
  5. Finally, the > 0 comparison will return true if there are any open child cases.
0

You can use a casedb instance expression to search for any open child cases of the current case who are the 'pregnancy' case type.

Step 0: To make your life easier, and your expression cleaner, I recommend creating a hidden value that stores the current case's case_id. The calculate condition for that is: instance('commcaresession')/session/data/case_id (which will be referenced as case_id in step 2 below. You can find this instance path on the CommCare Session Instance help page.

Step 1: Every instance expression that needs to search through existing cases (i.e. the 'case database(db)') starts like this: instance('casedb')/casedb/

Step 2: Then, you want to specify that you're looking for all cases whose parent is the current case: case[index/parent = case_id]

Step 3: Specify that among all child cases, you want only those of the 'pregnancy' case type: [@case_type = 'pregnancy']

Step 4: And you only want pregnancy child cases that are currently open: [@status = 'open']

That gives you: instance('casedb')/casedb/case[index/parent =  case_id][@case_type = 'pregnancy'][@status = 'open']

That will output all of the cases that match the criteria, but what you really want to know is how many exist.

Last step, step 5: add the count function around the instance path to see if you have any child pregnancy cases still open. If this is true, then it should count toward your indicator: count(instance('casedb')/casedb/case[index/parent =  case_id][@case_type = 'pregnancy'][@status = 'open']) > 0

1
  • 1
    Great minds think alike Commented Mar 1, 2017 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.