# Formulas for liquid users

Tips on how to use formulas for experienced liquid users.

Very little has changed here, if you’re using the no-code editor then liquid and formulas are indistinguishable for adding simple values.

If you switch over to editor mode, however, you will notice that the delimiter used in formulas has changed:

``````# liquid
{{ foo.bar }}

# formulas
<< foo.bar >>``````

## Calling a function​

Differences between liquid and formulas start to become apparent in how you call functions.

Where in liquid you would have written something like:

``````"abcd" | replace: "a", "b"
# Result: "bbcd"``````

... with formulas you now call a function in the same way you might in a spreadsheet application:

``````REPLACE("abcd", "a", "b");
# Result: "bbcd"``````

## Calling multiple functions​

Often you will want to call more than one function.

With liquid you only had one option for this:

``````"abcd" | replace: "a", "b" | replace: "c", "d"
# Result: "bbdd"``````

With formulas, you can use a nested function call:

``````REPLACE(REPLACE("abcd", "a", "b"), "c", "d");
# Result: "bbdd"``````

This, combined with the addition of some simple boolean functions, allows you to build much richer expressions than was ever possible with liquid:

``````IF(
is_ssl,
REPLACE("proto:www.mydomain.com", "proto:", "https://"),
REPLACE("proto:www.mydomain.com", "proto:", "http://")
);
# Result if is_ssl is true: "https://www.mydomain.com"
# Result if is_ssl is false: "http://www.mydomain.com"``````

Since nested function calls can quickly get pretty confusing, you can also use pipe syntax.

``````REPLACE(foo, "a", "b") |> REPLACE(%, "c", "d")
# Result: "bbdd"``````

A major improvement over liquid here is that you can use `%` to decide where the value from the left side of the pipe is used on the right side. Again, this allows you to write expressions that just weren’t possible before.

## as_object​

Formulas have no direct equivalent to `as_object`. Instead a field is either in “text mode” or “single value mode”.

### ​

In text mode, any values you use are automatically converted to text.

So if `results` is an array then the payload output by this action will include a version of `results` converted to text:

``````# results = ["1", "2", "3"]
{
"message": "[\"1\",\"2\",\"3\"]"
}``````

### Single value mode

In single value mode you can only enter a single formula, and the type of the result of evaluating that formula is retained.

So with our example from above we will now get the array in our output:

``````# results = ["1", "2", "3"]
{
"message": ["1","2","3"]
}``````

Under the hood single value mode is represented like so:

``````{
"message": "=results"
}``````

The no-code editor will automatically set some inputs into single value mode where it makes sense, for example when setting the object to be exploded in an event transform action.

## capture and assign​

There is no direct equivalent for capture and assign. Instead you can use `LOCAL` to break apart complex calculations or share data between multiple fields:

Here the options in `LOCAL` will be computed first from top to bottom and then will be a available in all other actions.

## IF and FOR​

Formulas retains `if` and `for` tags for use in templating. There are some minor quality of life improvements here you can write arbitrarily complex expressions for both of these so:

``````for foo in WHERE(slack_users.body.members, "deleted", FALSE)

if SIZE(WHERE(slack_users.body.members, "deleted", FALSE)) > 1``````

Also, as briefly mentioned above, there are now boolean functions which should help simplify your use of `if` tags

``````IF(thing1, "bar", "baz");

IF(AND(thing1, thing2), "bar", "baz");

IF(OR(thing1, thing2), "bar", "baz");``````

## Usage Examples​

Get the Current Date and Time in yyyymmdd Format

``````# liquid
"now" | date: "%Y%m%d"

# formula
DATE("now", "%Y%m%d")``````

Get time 24hrs ago

``````# liquid
"now" | date: '%s' | minus: 86400 | date: '%Y-%m-%dT%l:%M:%S%z'

# formula
DATE(DATE("now", "%s") - 24 * 60 * 60, "%Y-%m-%dT%l:%M:%S%z")``````

Get the Size of an Array from an Incoming Event

``````# liquid
my_array | size;

# formula
SIZE(my_array);``````

Multiply a Number in an Incoming Event by 10

``````# liquid
my_name | times: 10

# formula
my_number * 10``````

Get the Size of an Array and Multiply by 10

``````# liquid
my_array | size | times: 10
# formulas
SIZE(my_array) * 10``````

Generate a random number between 1 and 10

``````# liquid
{% random 1-10 %}

# formulas
RANDOM(1, 10)``````

Select all the elements in an array where the key has the given value

Input:

``````[
{
"color": "red",
"index": "1"
},
{
"color": "blue",
"index": "2"
},
{
"color": "green",
"index": "3"
}
]``````
``````# liquid
my_array | where: "index", "1"

# formulas
WHERE(my_array, "index", "1")``````

Output:

``[{ "color": "red", "index": "1" }]``