Skip to content
  • There are no suggestions because the search field is empty.

Expression step reference

Getting the most out of your data calculations

After reading this article, you’ll know the following:

  • How to configure the Expression step

  • What kind of expressions you can currently use

Expression step can be found in the Block Store and is used to create data formulas that help developers interact with various properties in their applications. Their values are changed depending on how the expressions are calculated.

 

Note: Some of these data formula steps have already been created. Have a look at this list to see what steps are already available in the Block Store

After installing, drag the Expression step to its desired spot in the action flow and configure it.

We have a video about the basics of expressions as well that you might want to check out:

 

Configure the Expression step

You can configure the Expression step by adding variables to your action in the formats shown below. Select the variables you want to use in the value selector fields and assign each one a key name. In your expression, reference these keys by wrapping them in double curly brackets to build your custom formula.

 

Example:

Variables: 

Key Value
first_name Odin
last_name van Eijk
prefix Dr.

Result: full_name: Dr. Odin van Eijk

 

Output type

In the result field, you can select whether you want the output type of the calculation to be provided to you in a text, boolean, number, object, or array variable.

After you select text as the output type, use the 'Result' field to define the name of the expression result. This result will then be available to you in the rest of your expression.

Example:

Variables:

Key Value
number_1 3
number_2 7

Result: multiplied_result: 21

 

List of available expressions

This is a list of expression formats that we currently support.

Text concatenation

For simple text concatenations with two variables, such as First name + Last name, you can also use this action step from the Block Store.

 


 

Advanced text concatenation

("{{ first_name }} " + ("{{ last_name_prefix }} " + "{{ last_name }}").trim()).length < 256 ? ("{{ first_name }} " + ("{{ last_name_prefix }} " + "{{ last_name }}").trim()) : {{ last_name }}

Variables:

Key Value
first_name John William
last_name_prefix De
last_name Graaf

Result (output type: Text): "John William De Graaf"

 


 

Ternary operator (if then else)

"{{ index }}" == 0 ? "{{ value }}" : "{{ some_other_value }}"

Output type: Depending on value

"{{ value }}" ? "{{ value }}" : "{{ some_other_value }}"

Output type: Depending on value

 


 

Present ternary

"{{ value }}" ? true : false

Output type: Boolean


 

Always return a numeric value

​{{ requested_id_value | 0 }}

Output type: Number

Result: Either a number from an existing ID or 0. This will prevent your action from going into error.

 


 

Ternary operator within a loop event

"{{ index }}" == 0 ? "{{ role_name }}" : "{{ current_role_names }}" + ", " + "{{ role_name }}"

Result (output type: Text): "Admin, Employee, Manager"

 


 

Concatenate collection, comma-separated

"{{ roles.map(item => item.name) }}"

Result (output type: Text): "Admin, Employee, Manager"

 


 

Concatenate collection, custom separator

"{{ roles.map(item => item.name) }}".replaceAll(",", "|")

Result (output type: Text): "Admin|Employee|Manager"

 


 

Concatenate filtered collection, custom separator

"{{ roles.filter(item => item.id < 3).map(item => item.name) }}".replaceAll(",", "|")

Result (output type: Text): "Admin|Employee"

 


 

Get URL and name from a file property

"{{{ file.url }}}"

Result (output type: Text): "https://assets.bettyblocks.com/.../…/example.pdf"

"{{{ file.name }}}"

Result (output type: Text): "example.pdf"

 


 

Using Regex

Regex expressions use Regex formatting in combination with JavaScript to filter results from text, and if the filtered result is found, it will be returned to your action step as the result. To learn how you can define Regex, there as websites like Regexr, that can help you create the perfect Regex condition for your use case.

When working with conditional whitespaces, make sure to double the "\" in your expression. For example, when a zip code could be either 1234AB or 1234 AB . The "\\s" option will allow you to filter when a whitespace is or isn't allowed to be there.

(Regex) replace

String.prototype.replaceAll() - JavaScript | MDN

"{{ value }}".replaceAll("r", "t")

Variables:

Key Value
value Berry Blocks

Result (output type: Text): "Betty Blocks"

 


 

Strip HTML

"{{{ value }}}".replace(/<[^>]*>?/gm, '')

Variables:

Key Value
value <html><div><p>Betty Blocks</p></div></html>

Result (output type: Text): "Betty Blocks" 

 


 

Truncate

"{{ value }}".length > {{ limit }} ? "{{ value }}".substring(0, {{ limit }}) + '...' : "{{ value }}"

Variables:

Key Value
value Betty Blocks
limit 8
Result (output type: Text): "Betty Bl..."
 
 

 

Calculations

Sum

{{ index }} + 1

Result (output type: Number): 3

 


 

Multiply

{{ index }} * 2

Result (output type: Number): 4

 


 

Count collection

You can use this action step from the Block Store to count the number of records in a collection based on the take. The default take is 50, and the maximum is 200. 

 


 

Sum collection

The total price of a collection of prices (for example, an invoice)

{{ orderlines.map(item => item.line_price).reduce((a, b) => a + b, 0) }}

Result (output type: Number): 5

 


 

Date formats

Date today

new Date().toISOString().substring(0,10)

Result (output type: Text): "2023-01-27"

 


 

Date formatting

new Date("{{ date }}").toLocaleDateString("{{ locale }}", {{ options }})

date is an ISO date.

locale is a string with a BCP 47 language tag.

Optional options can be configured - look here for more information. The options can be configured in such a way that basically everything from the date is outputted in a locale of your choice. As an example:

new Date("2022-12-23T08:41:50Z").toLocaleDateString("nl-NL", { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' })

Result (output type: Text): "vrijdag 23 dec. 2022"

 


 

Beginning of month

Returns the first day of a given month.

(() => {
  const dateObj = new Date("");
  return new Date(dateObj.getFullYear(), dateObj.getMonth(), 1).toLocaleDateString("en-CA");
})();

Variables:

Key Value
date 2025-02-18

Result (output type: Text): "2025-02-01"

 


 

End of month

(() => { 
const dateObj = new Date("");
return new Date(dateObj.getFullYear(), dateObj.getMonth() + 1, 0).toLocaleDateString("en-CA");
})();

Variables:

Key Value
start_date_at 2025-02-18

Result (output type: Text): "2025-02-28"

 


 

Others

Simple JavaScript function

It's also possible to use native JavaScript functions without interpolating values. In the example below, we are generating a random 5-digit code.

Math.floor(Math.random() * 90000) + 10000;

Result (output type: Number): 65427

 


 

Loop through collection with results

"<ul>{{#names}}<li>{{ name }}</li>{{/names}}</ul>{{^names}}Sorry, no people to list!{{/names}}"

Variables:

Key Value
names [{ name: "John Doe" }, { name: "Chuck Norris" }]

Result (output type: Text): "<ul><li>John Doe</li><li>Chuck Norris</li></ul>" 

 


 

Loop through collection with results + function

"<ul>{{#names}}<li>{{../fullName}}</li>{{/names}}</ul>"

Variables:

Key Value
names [{ firstName: "John", lastName: "Doe" }, { firstName: "Chuck", lastName: "Norris" }] 
fullName function() { return this.lastName + ", " + this.firstName; }

Result (output type: Text): "<ul><li>Doe, John</li><li>Norris, Chuck</li></ul>" 


 

 

Loop through collection without results

"<ul>{{#names}}<li>{{ name }}</li>{{/names}}</ul>{{^names}}Sorry, no people to list!{{/names}}"

Variables:

Key Value
names [] 

Result (output type: Text): "Sorry, no people to list!" 

 


 

HTML escaped value

"{{ customer_name }}"

Variables:

Key Value
customer_name Betty & Blocks

Result (output type: Text): "Betty &amp; Blocks"

 


 

HTML unescaped value

For example, to decode special characters from a response.

"{{{ customer_name }}}"

Note the triple curly brackets in the above expression.

Variables:

Key Value
customer_name Betty & Blocks

Result (output type: Text): "Betty & Blocks" 

 


 

Hide comments

"<p>This is shown!{{#show}} Psst, this is never shown{{/show}}</p>"

Variables:

Key Value
show false

 

 

 

Result (output type: Text): "This is shown!"

 


 
 

Show comments

"This is shown!{{#show}} And, this is also shown{{/show}}"

Variables:

Key Value
show false

Result (output type: Text): "This is shown! And, this is also shown" 

 


 

Advanced format option

Format phone number

Format a phone number to a certain format, in this format, the first 3 letters are separated (the location code) and surrounded by brackets ().

function formatPhoneNumber() { 
// Return empty text if there is no phone number
if (!"") return "";

// Only get numbers from text
const onlyNumbers = "".replace(/[
^0-9]/g, "");

// Format phone number like "(123) 456-7890"
return "(" + onlyNumbers.substring(0, 3) +") " + onlyNumbers.substring(3,6) +"-" + onlyNumbers.substring(6, 10);
}();

Variables:

Key  Value
office_phone 1234567890

Result (output type: Text): “(123) 456-7890”

 


 

 

Monday of week

Retrieve the date of a Monday from a week and year you provide.

function getMondayOfWeek() { 
const date = new Date(2025, 0, (1 + ( - 1) * 7));
const offset = date.getTimezoneOffset();
return new Date(date.getTime() - (offset * 60 * 1000)).toISOString().split('T')[0];
}();

Variables:

Key Value
year 2024
week_number 25

Result (output type: Text): “2024-06-17”

This date value can be saved in a date property and can be used further in the action flow.

 

 

Get element from array

Get a specific element from an array given an index (number).

Important to know that the index count starts from 0, therefore index 1 will return the second value from your array.

“{{ array }}“.split(“,”)[{{ index }}]

Variables:

Key Value
array [“Element 1", “Element 2”, “Element 3"]
index 1
Result (output type: Text): “Element 2”

Get array element - custom expression

 


 

Filter record from collection

This filter expression is used to filter an element from a (custom) collection. In this example, the output type array is used.

The expression itself is a good example of how you can deal with collections in the Expression step.

[{{ collection.map(item => item.id) }}].filter(item => item !== {{ record_id }})

Variables:

Key Value
collection [{ id: 1, title: “Element 1" }, { id: 2, title: "Element 2" }, { id: 3, title: “Element 3” }]
record_id 2
Filter record from collection expression example

Result (output type: Array): [{ id: 1, title: “Element 1" }, { id: 3, title: “Element 3” }]


Some expressions have also been turned into a block. Have a look at this list to see what steps are already available in the Block Store.

If you have any expression formatting you would like to report and add to the list, reach out to us via email: vitalii.nemchenko@bettyblocks.com