# Conditions

Conditions are used to compare values at runtime and add dynamic behavior to your apps. They are configured in the same way as [filters](https://docs.appfarm.io/reference/platform-concepts/filters), but a condition will always return a true or false value. Conditions can be implemented when designing the [UI](https://docs.appfarm.io/reference/apps/ui) (for example to set the **Visibility** property of a component), or building logic in [actions](https://docs.appfarm.io/reference/apps/actions) (for example in an [If action node](https://docs.appfarm.io/library/action-nodes/if)).

To build conditions, you should also be familiar with [operators](https://docs.appfarm.io/reference/platform-concepts/operators).

## Condition editor

Conditions are constructed using the condition editor. The most basic condition is a single expression made up of a *left operand*, an *operator*, and a *right operand*.

In the example below, the left operand is `Ticket.Status`, the operator is `Equals` and the right operand is `New`. The condition is evaluated at runtime and if the status of the ticket is new, the condition will return true.

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FkUgPgD03wsF3nS2EJx3Z%2Fcondition_basic.png?alt=media&#x26;token=32e75114-8834-454b-bdbc-78e34dba99c7" alt=""><figcaption><p>A basic condition with one expression</p></figcaption></figure>

A condition can also consist of multiple expressions. When you add an additional expression the new expression is compared to the previous expression with an **AND** operator. This means that *both* expressions must equate to true for the condition to return true. You can click the operator to toggle to **OR** and in that case, if *either* expression equates to true the condition will return true.

In the example below, the ticket status must be new *and* the description must contain a value (not empty or null) for the condition to return true.

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2Fh5vL4mttnlzDK5QojBaW%2Fcondition_multiple.png?alt=media&#x26;token=55d92c52-5ce6-41cf-9f4b-582dc912f7a5" alt=""><figcaption><p>A condition with multiple expressions.</p></figcaption></figure>

Expressions can be combined within *groups*, and the result of the expressions within a group can be compared.

In the example below, the ticket status must be new and the description must contain a value (not empty or null) *or* the ticket must have been created prior to the start of the 2022. If either of these equate to true the condition will return true.

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2F3ZOhY7nZAjMceqenm15Y%2Fcondition_group.png?alt=media&#x26;token=96e580ad-43cc-4c23-9c3b-729b52e1debc" alt=""><figcaption><p>A condition with a group of expressions</p></figcaption></figure>

You can add a comment to a condition, and it can be useful to describe how a condition works when it contains many expressions.

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FHruD4IqIsGdbO5wJ6jyu%2Fcondition_comment.png?alt=media&#x26;token=ea8b787f-82dd-45ce-a6fc-9653ff1c6e3c" alt=""><figcaption><p>A condition with a comment</p></figcaption></figure>

### Cardinalities of left and right side operands

The right operand may be a single value/object or a list of values/objects, whereas **the left operand may only be a single value or object,** except in the case where the left operand is a property with [Cardinality Many](https://docs.appfarm.io/data-model/object-class-properties#general-properties) (see example 5)**.**&#x20;

Here are some example setups of valid conditions, given the following data sources:

* `Person` (Cardinality *One*)
  * With a property `Department` (with cardinality One)
  * With a property `Tags` (with cardinality Many)
* `Departments` (Cardinality *Many*)
* `Tags` (Cardinality Many)

#### Example 1

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FBimIF3vUWWgSHvWZ3sZE%2FCondition%20example%201.png?alt=media&#x26;token=bfadc1d8-dd39-4b53-82b9-78e345237a65" alt=""><figcaption><p>Conditions example with Single cardinality on both sides</p></figcaption></figure>

In the above example, the left operand is a single value. Since the `EQUALS`-operator is used, the right operand must be a single value. **The right operand is treated as a single value** if the `Departments` Data Source has a single [selected object](https://docs.appfarm.io/reference/objects#object-selection), or if the Departments Data Source has a [context object](https://docs.appfarm.io/reference/objects#object-in-context) (i.e. this condition is placed inside an iterator, such as inside a Foreach action node, looping the Departments Data Source).

#### Example 2

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FYzWvc3V4a8hOfTPiEuP1%2FCondition%20example%201b.png?alt=media&#x26;token=0da62472-0601-479e-bbad-0484d53462fb" alt=""><figcaption><p>Conditions example with Single cardinality on both sides</p></figcaption></figure>

This example is similar to example 1, but **the right side is a single property** itself. App Variables is in fact a single cardinality Data Source, and we've added a variable `Selected Department` to it.

#### Example 3

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FN7UHUBHWl3UNsyPCa4E6%2FCondition%20example%202.png?alt=media&#x26;token=78c59e68-6e50-444f-aa6b-2b45dcff2bad" alt=""><figcaption></figcaption></figure>

In the above example, the left operand is a single value, similar to the previous examples. However, since the operator `EXISTS IN` is used, **the right side is multiple values**. In this case, the right side is a list of the IDs of the selected object in the `Departments` Data Source.

#### Example 4

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FurkrPYwVhDDK43i7PcOB%2FCondition%20example%204.png?alt=media&#x26;token=e62fced0-e276-4310-85c4-a8dd40d9bff5" alt=""><figcaption></figcaption></figure>

In this example, the left operand is still a single value. However, since the Departments Data Source has cardinality many, the above expression is only valid if the `Departments` Data Source has a single [selected object](https://docs.appfarm.io/reference/objects#object-selection), or if the Departments Data Source has a [context object](https://docs.appfarm.io/reference/objects#object-in-context) (i.e. this condition is placed inside an iterator, such as inside a Foreach action node, looping the Departments Data Source).

#### Example 5

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FThJ1XyfSzZwQJ5Wn6PFb%2Fimage.png?alt=media&#x26;token=89426a94-9c06-423e-9c45-f086586a3557" alt=""><figcaption></figcaption></figure>

In this example, we have a condition towards the multi-reference property `Person.Tags`. You may read more about multi-references [here](https://docs.appfarm.io/how-to/data-modeling/many-to-many-relationships) if you are new to the concept, but in general, it is just a list of IDs (referencing 0 to many `Tags` in this example).

In the above condition, we check if the `Person.Tags` list contains some of the selected objects in the `Tags` data source.

You may read more about these multi-reference operators (Has All Of, Has Some Of, Has None Of) [here](https://docs.appfarm.io/reference/operators#logical-operators).

## Advanced conditions

In some cases, it may not be possible to create the condition you need in the condition editor. If so, you can instead write a function that returns either `true` or `false`. See [Advanced functions](https://docs.appfarm.io/reference/functions#advanced-functions) for examples of advanced conditions.
