Welcome to Common Logic and Calculations!
This page contains descriptions of advanced techniques and examples for you to use when building CommCare Forms.
Before diving into Logic and Calculations, we recommend you take a moment to read our article on Form Builder and review the Beginner Tutorial.
Display conditions, sometimes also called skip logic,are used to determine when a particular question should be shown to a user. While you can use the Form Builder to make basic display conditions, it is also possible to create very complex expressions.
For more information please visit the Display Conditions page
Validation Conditions are used to require that a user enters a certain type of response. A simple example could be requiring that a date be in the future; simple expressions can be added using the Form Builder Expression Builder. However it is also possible to write very complex expressions which require a specific structure. A complex example would be requiring a code which is 5 digits long and starts with a letter.
You can also put a question-specific validation message which tells the user why their response did not meet the validation condition.
Basic Validation Conditions - this page provides some basic examples of validation expressions, including the use of dates and numeric ranges.
Advanced Validation Conditions - this page provides examples and guidelines for complex validation expressions, such as requiring a specifically formatted number or series of letters.
- Make a subset of questions required - describes how to configure a set of questions such that only a certain number are required, and the user can choose which ones to answer.
One of the most powerful features of CommCare is the ability to do calculations within a form by working with a special type of question called a hidden value. If you are just getting started using hidden values we recommend that you try going through the Hidden Value Calculations Tutorial.
- Calculations in Hidden Values - an basic overview of common calculations
- Troubleshoot Calculations - have lots of complex calculations that you want to test? This page has some suggestions for you!
- Join two values ("concatenate") - example of a function which joins together two questions. For example if you have a question for first name and another for last name and want to join them into one property you can use the concatenate function.
- Generating a Random Number - examples for using the random() function in CommCare calcluations
- Rounding numbers - this page provides some examples of how you can round the input to numeric questions
- Calculations with Dates and Times - key resource for doing calculations with date or time questions
- "If" Statements - one of the most powerful tools in building CommCare calculations, this page provides examples of how if statements can help you to synthesize inputs from several questions into one output
Logic for Specific Question Types
Some question types are a bit more complex than others! These pages provide examples and explain how to work with some of these question types.
Advanced Form Design
This section has pages that you might want in order to build complex workflows or structures into your forms. Note that pages in this section may require knowledge of Case Management, a key feature of CommCare which is used to link forms together.
- Link Questions Between Forms - this page gives a brief overview of how to reference previously collected information in a form. More detail is also available in Case Management
- Increment a counter - technique for keeping track of the number of times a form has been filled out. This is often used to count the number of visits or other type of interaction.
Create a Score Tally - if you have a series of questions and want to tally a "score" this page will provide some useful guidance
Displaying History of Changes - if tracking the changes in one property over time you may want to concatenate them over time in order to make it easy to refer to
Defining a Default Value - describes how to set a default value for a question
Available Functions - this page provides detailed descriptions and examples of all the possible CommCare Functions that you can use when making calculations or other expressions. Common examples of functions include: today(), string-length(), now(), and date() - these are very useful for manipulating data.
Show the username in the case list/details - this technique is important if you want to be able to show the username of the person who created or last interacted with a case in the case list or case details. This is primarily useful in applications that use Case Management.
Display an Answer to a Previous Question - this page explains how to show the display a calculation of another question in the label text of another question.
Wondering where "/data/" went?
On July 5, 2016, we released an update to the application builder that changes the way you reference form questions. To read more about the new references, see this overview
How do we check if the LMP date is 3 months less than the current date?
I have created a Date Field to accept the LMP date. How do we check if the LMP date is 3 months less than the current date?
Then, I create a label and under logic, in the "Display Condition," I enter the following code: int((today() - (/data/imp + 90)) div 30.4) to determine the number of months from the user-entered LMP date. Is it correct?
For questions and support, please use the "Report an Issue" button. You can fnd t at the bottom of every page on CommCareHQ.
I aggree to amelia idea
Je manifeste mon intérêt et engagement de faire chemin avec vous. et trouve très intéressants vos outils
Je m'engage à travailler dans votre structure, parce que ce que vos actions sont nobles et très humaines. Nous vous remercions pour tout le bien que vous êtes entrain de réaliser en faveur de l'être humain.
That's really great ... I appreciate it a lot.
Bravo for this fight for a noble cause