LibraryDocsBlogPricing
Log inSign up for free →

Expressions and conditionals

On this page
  1. 1. Using & writing expressions
  2. 2. Available variables
  3. 3. Expression functions
  4. 4. Examples

You might commonly want to run actions or workflows when criteria are met. For example, if you're subscribing to all Shopify order events you might only want to run workflows if the order total is over $500. This is built into the platform via expressions.

Expressions work by testing statements to see if they're true. For example, "hi" == "hi" is true; hi is equal to hi. Using the power of expressions, you can check any part of your event data, user data, or action data to conditionally run parts of your workflow.

Using & writing expressions

Expressions can be created via the UI and written as a common "IF" expression. Here's a small example:

event.data.total >= 500

This true if the value of "total" within the event is greater than or equal to 500.

Available variables

These variables are always available:

  • event, which contains event data
  • action, which contains action results. This is used in the same way as templating, eg. action.1.status to refer to the "status" field in the output of action 1.
  • user, which contains user information

The following are available in specific circumstances:

  • response, used in edges to refer to the previous action's response
  • async, used when waiting for another event in a workflow to refer to the new event's data

response

If you're testing an edge, you can use the shortcut response to refer to the action that just completed. For example, if you have action 1 which leads to action 2, the response variable in this edge refers to action.1.

async

If you're waiting for another event by using the built in "Wait for event" action, the new event's data is available in the async variable. This lets you compare the new event you just received to the event that originally kicked off the workflow.

Expression functions

String functions (for text)

$.startsWith: whether the string starts with the given text
event.data.name.startsWith("a")

$.endsWith: whether the string ends with the given text
event.data.name.endsWith("a")

$.contains: whether the string contains the given text
event.data.priority.contains("urgent")

$.matches: tests the string against a given regex. Note, backslashes must be escaped as the regex is written as a string ("\\d" is equal to "\d").
event.data.priority.matches("^[a-zA-Z\\d]{0,10}$")

uppercase($): converts the given string to uppercase
uppercase(event.data.priority)

lowercase($): converts the given string to lowercase
lowercase(event.data.priority)

Array functions

$ in []: whether an item exists in an array
event.data.priority in ['P1', 'P2']

size($): returns the size (length) of an array
size(event.data.priority)

$.exists: iterates over all items in an array to test whether at least one element matches an expression, assigning to x and testing the expression
[1, 2, 3].exists(x, x >= 2) $.exists_one: iterates over all items in an array to test whether exactly one element matches an expression, assigning to x and testing the expression
[1, 2, 3].exists_one(x, x == 3)

$.all: iterates over all items in an array to ensure that all items match an expression, assigning to x and testing the expression
[1, 2, 3].all(x, x >= 1)

$.map: iterates over all items in an array and applies a function, returning the result
[1, 2, 3].map(x, x * 2)

$.filter: iterates over all items in an array and applies a function, returning only the items that match the expression
[1, 2, 3].map(x, x >= 2)

Time, date, and duration functions

date($): casts a string or int to a date for date comparisons. Accepts dates in all common formats, including RFC3339 (with or without timezones), RFC822, Unix formatting, Ruby formatting, ANSIC, "YYYY-MM-DD", unix timestamps, and unix timestamps in seconds.

now_plus($): Returns a date extended by a duration (in months, weeks, days, hours, or seconds)
date(event.data) < now_plus("2w3d12h") // 2 weeks, 3 days, 12 hours

now_minus($): Returns a date minus a duration (in months, weeks, days, hours, or seconds)
date(event.data) < now_plus("12h30m10s") // 12 hours, 30 minutes, 10 seconds

Examples

js
event.data.total >= 500 && event.data.items.exists(x, x.price >= 100)
// Given the following data:
// {
// "event": {
// "data": {
// "total": 598.99,
// "items": [
// { "name": "Onyx Speaker", "price": 498.99 },
// { "name": "Pi Sync", "price": 100 },
// ]
// }
// }
//
// This will evaluate to true
js
// Check that one of the tags, when lowercased, is either p0, p1, or p2
event.data.tag.map(t, lowercase(t)).exists(t, t in ['p0', 'p1', 'p2')
// This could also be done using match:
event.data.tag.exists(t, t.match("/[pP][0-2]/"))
// Given the following data:
// {
// "event": {
// "data": {
// "tags": ["Bug", "P0"],
// }
// }
//
// This will evaluate to true

Async event coordination:

js
async.event.data.subject == "Reply" && '{{ event.data.subject }}' == "Sent:"
    On this page
  1. 1. Using & writing expressions
  2. 2. Available variables
  3. 3. Expression functions
  4. 4. Examples