Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 update the case with the form data /data/save birth_facility In the follow up form create a hidden value called something like birth_facility_previous and leave it blankto 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/data/change_facility = 'yes', #form/data/birth_facility_new, #case/data/birth_facility_previous)
  • In the case management section of the follow up form settings, make sure to "load" /data/birth_facility_previous with the case property birth_facility (from the registration form)
  • In the case management section of the follow up form settings, make sure to use the form data /data/birth_facility to update the case property birth_facility

For further guidance, please see the Hidden Values Tutorial Part 3: If Statements

 

...

Operation ordering

Icon

Expressions in CommCare are evaluated left to right. The following example illustrates how this impacts expressions:

...

 

A

...

and

...

B

...

or

...

C

 

is equivalent to:

...

 

((A

...

and

...

B)

...

or

...

C)

 

Should you wish to evaluate expressions differently you must use parentheses to indicate which elements should be evaluated together e.g.:

...

 

A

...

and

...

(B

...

or

...

C)

 

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/data/sys_bp >= 120 and #form/data/sys_bp <=139, 1, 0) where 1 refers to high risk

If "Or" Statement

Example with OR: if((#form/data/gender = "male" and #form/data/waist_circumference > 90) or (#form/data/gender = "female" and #form/data/waist_circumference > 85), 1, 0) where 1 refers to a high risk

...

Argh! I can't figure out what the mistake I made is!

Icon

You might get a generic error message and not understand why your complex statement is not working. Here are some common sticking points:

  • Check all of your parentheses and quotation marks! They must all be in pairs. If you can't find it, try copying the statement into a tool like Notepad++ or Sublime Text as these tools can help you find missing parentheses
  • Check your spellings!

...

  • Anytime you use "and" or "or" they must be lowercase