Versions Compared

Key

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

Excerpt

This page provides detailed descriptions and examples of all the possible CommCare Functions that you can use when making calculations or other expressions, which can be useful in manipulating data.

...

  • Behavior:  Evaluates a value against a regular expression and returns true if the value matches that regular expression.

  • Return: true or false

  • Arguments:  There are two arguments, the value to be validated and the regular expression as a string. 

  • Syntax: regex(value, regular_expression)

  • Example:  This is useful when doing complex validation against some value.  For example, to validate that a string contains only numbers, you can use regex(/data/my_question, "^[0-9]+$").   You can test and develop other regular expressions using http://www.regexr.com/.  Also see the Advanced Validation Conditions page.

uuid()

  • Behavior:  Calculates a random hexadecimal (0-9, a-f) identifier of 32 characters long.  There are 16^32 = 3.4*10^38 possibilities for this kind of identifier.  The statistical chance that these values are not unique is extremely low, so we call it a unique identifier.  https://en.wikipedia.org/wiki/Universally_unique_identifier

  • Return: The unique id.  

  • Arguments:  none!

  • Syntax: uuid().  Omitting the argument results in a 32 digit unique identifier.

  • Examples: uuid() could return 24235c71-09bf-40e8-87d2-00767efd7a14 or 4498b8a9-0af4-4413-994a-ad44e166f073

uuid(argument)

  • Behavior:  Calculates a random alphanumeric (0-9, A-Z) identifier of a particular length.  The longer the length, this more likely it is that this random number is unique across the project.  If the argument is 3, then there are 36^3 = 4.7*10^4 possibilities.  This is a large number of possibilities, but not large enough to ensure that there is not a duplicate.  If the argument is 32, there will be 36^32 = 6.3*10^49 possibilities, which is statistically very unlikely to produce a duplicate.  Note that uuid() with an argument is not a traditionally-defined UUID as described here; https://en.wikipedia.org/wiki/Universally_unique_identifier.

  • Return: The random id.  

  • Arguments:  The length of the random id

  • Syntax: uuid(length).

  • Examples: uuid(4) could return WZV4 or 5J43.  uuid(32) could return 4KV5JRAUM48YS9SP2SWX2G94UEAJBHXQ or N9HTXSZPJI0H8GQS2SBW88V881CJEN1I.

coalesce

  • Behavior:  Useful for choosing which of two values to return.  Will return the non-null/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"). 

  • Note: Since CommCare version 2.31, coalesce accepts more than 2 arguments, returning the first non-null argument.

...

  • Behavior: Takes a json string and a string property name and returns the value of that property from the json string.

  • Return: A string value for the property name passed in 

  • Arguments: A string (stringified json object), a string property name

  • Syntax: json-property(json_string, property_name)

  • Example: json-property('{"full":"New Canada St., 3855 Brienz, Switzerland","geopoint":"46.748107 8.0473685","zipcode":"3855","postcode":"3855","city":"Brienz","region":"Bern","state_long":"Bern","state_short":"CH-BE","country":"Switzerland","country_short":"ch","street":"New Canada St."}', 'city') -> "Brienz"

  • Since: This function is available in CommCare 2.51 and later.

...

  • Behavior: 2 strings, each of which should be a space-separated string representing a list of strings, and an optional boolean value indicating the desired sort direction, and returns the result of sorting the 1st list by the 2nd list.

  • Return: A string representing a sorted, space-separated list

  • Arguments: 2 strings representing space-separated lists, and an optional boolean argument indicating direction (true() is ascending, false() is descending, default is ascending)

  • Syntax: sort-by("a b c d", "3 2 4 1", true())

  • Examples: 

    • sort-by("apple banana chip dog egg flea", "4 2 1 5 3 2") --> "chip banana flea egg apple dog" 

    • sort-by("apple banana chip dog egg flea", "4 2 1 5 3 2", false()) --> "dog apple egg flea banana chip" 

  • Since: This function is available on CommCare 2.38 and later

...