It may be useful to increment counters in a case values, the typical example being the number of visits or consultations a patient received. The case stores the number of consultations received and each form stores a number of the consultation received at that time. Such counters are implemented with a couple of hidden values in the form and a single case value.
Let's take an example of Ante Natal Care (ANC) visits. In this example the form has these hidden values:
- Use a hidden value called
/data/previous_anc_numberto get the number of ANC already received by the patient. If the patient is experiencing her third ANC, this value is 2. Load this hidden value from the case property called
- Use another hidden value (
/data/anc_number)to compute the current ANC number
byadding 1 to
- Since data
/previous_anc_numbercan be blank in the case of a first ANC, we need a more complicated Calculate Condition of
coalesce(/data/previous_anc_number, 0) + 1
- We then save /data
/anc_numberto the case, using the case property name
CommCare Functions: Coalesce
This example uses a function called coalesce. You can read more about CommCare Functions but here is the basic information about how this tool works:
- Behavior: Useful for choosing which of two values to return. Will return the non-empty value. If both are not null, will return the first argument.
- Return: One of the values
- Arguments: The two values to be coalesced
- Syntax: coalesce(value_1, value_2).
- Example: This is useful if you want to use a default value when referring to a question which may or may not have been answered. Ex. coalesce(/data/my_question, "my default value").
Explained Step by Step
To count or keep track of ANC visits in follow-up forms, do this…
- Create Hidden Value /data/previous_anc_number
- Create Hidden Value data/anc_number
- In /data/anc_number, in the display condition, type: coalesce(/data/previous_anc_number,0)+1
- Go to the Case Management tab for this form
- Under Save data to the following case properties, save both /data/previous_anc_number and /data/anc_number
- Under Load the following case properties into the form, load /data/anc_number as the Case Property and /data/previous_anc_number as the Question
Why do it like this?
- The hidden value, /data/previous_anc_number gives the number of ANC already received by the beneficiary. For example, if the beneficiary is experiencing her third ANC, this value would be 2. This value is loaded from the case property anc_number, ie the number visit number saved during the beneficiary’s last consultation.
- The hidden value (/data/anc_number) is used to compute the current ANC number by adding 1 to/data/previous_anc_number
- Since data/previous_anc_number can be blank in the case of a first ANC, we need a more complicated Calculate Condition of coalesce(/data/previous_anc_number, 0) + 1. Therefore, during the first visit, when previous_and_number does not have a value, it will be replaced by 0.
- We then save /data/anc_number to the case, using the case property name /data/anc_number
You can think of it like this…