
# HSL Expression Reference

An expression is one line that gives an answer: *does this record match?* or
*what is the value?* You write expressions in filters, list views,
validations, transitions, automations and formula fields. No programming is
involved: there are no loops and no steps, so every expression ends.

This is **HSL level 1**. When one line is not enough (look something up,
decide, write, notify), use a script:
[HSL Script Reference](hsl.html).

> Every example on this page is checked against Horizon itself, each time
> Horizon is built.

The examples use the same small model as the script reference:

| Type | Fields used here |
|---|---|
| `workOrder` | `name` (text), `priority` (number), `dueAt`, `closedAt` (date and time), `startsOn` (date), `category` (picklist), `requester` (user), `urgent` (yes/no), `building` (reference → `building`), `trades` (multi-picklist), `watchers` (multi-user), `takes` (duration) |
| `person` | `name` (text), `openWorkOrders` (number), `building` (reference → `building`) |
| `building` | `name` (text), `city` (text) |

You write the **`apiName`** of a field, not its label. The names Horizon ships
with are listed in the data model on this site; the names of your own fields
are shown in Horizon.

## 1. Conditions

A condition is true or false.

```hsl condition
priority > 2
name == "Boiler room"
urgent == true
category in ["elevator", "hvac"]
priority between 2 and 4
priority > 2 and urgent == true
(priority > 2 or urgent == true) and closedAt == null
```

| You write | It means |
|---|---|
| `==` `!=` | equal, not equal |
| `<` `<=` `>` `>=` | less, at most, more, at least |
| `between a and b` | from `a` to `b`, both included |
| `in [a, b]`, `not in [a, b]` | one of these, none of these |
| `.contains("x")` | text: contains this, upper and lower case alike; lists: contains this value |
| `.startsWith("x")` | text: begins with this |
| `== null`, `!= null` | empty, not empty |
| `and`, `or` | both, at least one; `and` binds stronger |

Text goes in double or single quotes. There is no escape character: text that
contains a double quote goes in single quotes.

```hsl condition
name.startsWith("Hall 3")
name == 'Pump "North"'
trades.contains("hvac")
```

There is no general `not`. Write the opposite: `!=`, `not in` or `== null`.

### What fits which field

| Field kind | Comparisons |
|---|---|
| Text | `==` `!=` `in` `contains` `startsWith`, empty |
| Number, date, date and time, time, duration | `==` `!=` `<` `<=` `>` `>=` `in` `between`, empty |
| Yes/no | `==` `!=`, empty |
| Picklist, reference | `==` `!=` `in`, empty |
| Catalogue | `==` `!=` `in` `contains` `startsWith` (on the code), empty |
| Multi-picklist, multi-reference, multi-text, multi-user | `contains`, empty |
| JSON, location | empty |

`in` includes `not in`; “empty” is `== null` and `!= null`. For picklists and
catalogues you compare the **code** of the value, not its label.

## 2. Me, now and today

```hsl condition
requester == user.id
watchers.contains(user.id)
dueAt < now()
startsOn <= today()
```

`user.id` is the person who is looking. `today()` is the current day,
`now()` the current moment.

In transitions and automations no person is looking: do not use `user` there.

## 3. Dates

Add or subtract with `plus` and `minus`. Always say `calendar`.

```hsl condition
dueAt < now() plus 7 calendar days
startsOn >= today() minus 3 calendar months
```

Units are `minutes`, `hours`, `days`, `months` and `years`. Working time
(`working`) and `elapsed` are refused, so that no deadline is silently
calculated the wrong way:

```hsl refused expression.workingTimeBasisMissing
dueAt < now() plus 8 working hours
```

The time between two moments is a duration:

```hsl value Duration
closedAt - dueAt
now() - dueAt
```

## 4. Values and formulas

A formula field contains a value instead of a condition. Its result is a
text, number, date, date and time, duration or yes/no.

```hsl value Number
priority * 2 + 1
if(urgent == true, 4, 24)
round(priority / 3)
```

```hsl value Text
name & " (" & category & ")"
upper(trim(name))
if(closedAt == null, "open", "done")
```

| You write | It gives |
|---|---|
| `+` `-` `*` `/` | calculation; `*` and `/` first |
| `&` | joins texts (`+` only adds numbers) |
| `if(condition, a, b)` | `a` if the condition is true, otherwise `b` |
| `lower(text)`, `upper(text)`, `trim(text)` | lower case, upper case, without spaces at both ends |
| `length(text)` | number of characters |
| `abs(number)`, `round(number)` | without sign, rounded to a whole number |

Put a space after a minus sign. `priority - 1` is a calculation, `priority -1`
is not:

```hsl refused condition.comparisonExpected
priority -1 > 0
```

A formula cannot use another formula field, and `null` is not a value you can
return. Division by zero gives an empty result.

## 5. Related records

Follow a reference with `ref via`, or ask for records that point here with
`any … via`.

```hsl condition
ref via building where city == "Berlin"
```

On a building: is there an urgent work order?

```hsl condition building
any workOrder via building where urgent == true
(any workOrder via building where urgent == true) and city == "Berlin"
```

`where` takes everything up to the end of the line. Put the step in
parentheses, as in the second line, if something else follows.

To calculate over related records, use `count`, `sum`, `avg`, `min` or `max`
with `over`. Here `where` is optional.

```hsl value Number building
count(name) over any workOrder via building
count(name) over any workOrder via building where closedAt == null
max(priority) over any workOrder via building
```

Without any matching record `count` gives 0, the others give an empty result.
You only see results from records you are allowed to read. For structures
(a building below a site, a part below a machine) add `depth any` to include
everything below, not only the next level.

At most three steps can be nested.

## 6. Where you can use what

| Place | Type | Related records |
|---|---|---|
| Filter and list view | condition | yes |
| Transition | condition | yes |
| Automation (“which records”) | condition | yes |
| Validation | condition | no |
| Show or hide fields in a form | condition | no |
| Formula field | value | yes, with `count`, `sum`, `avg`, `min`, `max` |

## 7. When something is wrong

Horizon checks an expression before it is saved and points at the place:

```
`==` is probably meant — a single `=` assigns, and a condition cannot assign (character 11):
  priority = 2
            ^
```

A field you may not see is reported exactly like a field that does not exist.

An expression has at most 10 levels of nesting and 200 parts.
