Overview
There are certain scenarios when you need to create more complex logic (than available in the built in logic builder (e.g. X statement is equal to Y value)) or more complex calculations. One option is to create a custom IF statement. An IF statement follows the construction, IF this, THEN this, OTHERWISE this. The format to express this kind of logic is the following: IF(evaluative statement, result if statement is true,result if statement is false).
For example, you could use an IF statement to conditionally store a value in a Hidden Value. Let's take an example where you register a client (a case) in a registration form and update the case property birth_facility. Later in the Follow Up form you want to update the case property birth_facility to the value of the question birth_facility_new IF the answer to change_facility is 'yes'; otherwise you want to leave it as the original value from the registration form. You would do the following:
- Make sure that in the registration form you save birth_facility to the case
- In the follow up form create a second hidden value called birth_facility and enter an if statement in the "calculate" block: if(#form/change_facility = 'yes', #form/birth_facility, #case/birth_facility)
For further guidance, please see the Hidden Values Tutorial Part 3: If Statements
If "And" Statement
You might have multiple conditions for an IF statement, where you need to use the OR or AND functions. Below are examples that demonstrate how you can write your expressions. Note that there is no need to enter IF twice for each argument.
- Example with AND: if(#form/sys_bp >= 120 and #form/sys_bp <=139, 1, 0) where 1 refers to high risk
If "Or" Statement
Example with OR: if((#form/gender = "male" and #form/waist_circumference > 90) or (#form/gender = "female" and #form/waist_circumference > 85), 1, 0) where 1 refers to a high risk