After reading this article you’ll know the following:
How to configure the expression step
What kind of expressions you can currently use
The 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.
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 downloading, drag the expression step to its desired spot in the action flow and configure.
We have a video about the basics of expressions as well that you might wan tot check out:
How to configure the expression step
The expression step can be configured by using variables inside your action in a given format following the expression examples provided in the list down below.
Within the expression step you select the variables you want to use, in the value selector fields and assign a key name to them.
These keys can be assigned in the expression formatting by typing the key enclosed in double curly brackets in the format of the expression (see example down below).
Example:
Prefix | First name | Last name |
Dr. | Odin | van Eijk |
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, number, or boolean variable.
After you select text as 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:
Number 1 | Number 2 |
3 | 7 |
Result multiplied_result: 21
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 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 }}
Result (output type: Text):
John William Doe
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 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 type property of e.g. model Document
"
{{{ 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. When for example a zip code could be either 1234AB
or 1234 AB
, the "\\s" option in will allow you to filter when a whitespace is or isn't allowed to be there.
(Regex) replace
"{{ value }}".replaceAll("r", "t")
Input
Value:
{ value: "Berry Blocks" }
Output (type: Text):
Value:
Betty Blocks
Strip HTML
"{{{ value }}}".replace(/<[^>]*>?/gm, '')
Input
Value:
{ value: "<html><div><p>Betty Blocks</p></div></html>" }
Output (type: Text):
Value:
Betty Blocks
Truncate
"{{ value }}".length > {{ limit }} ? "{{ value }}".substring(0, {{ limit }}) + '...' : "{{ value }}"
Input
Value:
{ value: "Betty Blocks", limit: 8 }
Output (type: Text):
Value:
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 amount of records in a collection based on the take. The default take is 50 and the maximum 200.
Sum collection
{{orderlines.map(item => item.line_price).reduce((a, b) => a + b, 0)}}
Total price (output type: number)
Result: the total price of a collection of prices (for example, an invoice)
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}}) 2new Date("2022-12-23T08:41:50Z").toLocaleDateString("nl-NL", { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' })
vrijdag 23 dec. 2022
date is an ISO date.
locale is a string with a BCP 47 language tag.
Optional options can be configured see 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.
{ day: 'numeric'} = 23
Others
Loop trough collection with results
Input
Value:
<ul>{{#names}}<li>{{name}}</li>{{/names}}</ul>{{^names}}Sorry, no people to list!{{/names}}
Variables:
{names: [{name: "John Doe"}, {name: "Chuck Norris"}]}
Output
Output type: Text
Value:
<ul> 2 <li>John Doe</li> 3 <li>Chuck Norris</li> 4</ul>
Loop trough collection with results + function
Input
Value:
<ul>{{#names}}<li>{{../fullName}}</li>{{/names}}</ul>
Variables:
{names: [{firstName: "John", lastName: "Doe"}, {firstName: "Chuck", lastName: "Norris"}], fullName: function() { return this.lastName + ", " + this.firstName; }}
Output
Output type: Text
Value:
<ul> 2 <li>Doe, John</li> 3 <li>Norris, Chuck</li> 4</ul>
Loop trough collection without results
template: "<p>{{name}}</p><ul>{{#names}}<li>{{name}}</li>{{/names}}</ul>{{^names}}Sorry, no people to list!{{/names}}<p>{{name}}</p>"
variables: {name: "Chunk Norris", names: [{name: "Paul"}, {name: "Engel"}]}
Input
Value:
<ul>{{#names}}<li>{{name}}</li>{{/names}}</ul>{{^names}}Sorry, no people to list!{{/names}}
Variables:
{names: []}
Output
Output type: Text
Value:
Sorry, no people to list!
HTML escaped value
Input
Value:
{{customer_name}}
Variables:
{customer_name: "K & V Dak"}
Output
Output type: Text
Value:
K & V Daksystems
HTML unescaped value
Input
Value:
{{{customer_name}}}
Variables:
{customer_name: "K & V Dak"}
Output
Output type: Text
Value:
K & V Daksystems
Hide comments
Input:
Value:
<p>This is shown!{{#show}} Psst, this is never shown{{/show}}</p>
Variables:
{show: false}
Output:
Type: Text
Value: “This is shown!”
Show comments
Input
Value:
<p>This is shown!{{#show}} And, this is also shown{{/show}}</p>
Variables:
{show: true}
Output:
Output type: Text
Value: “This is shown! And, this is also shown”
Custom format option
Using your coding knowledge you can also create your own expression formatting. We have collected a handful of custom format expressions in this article.
Some custom data formatting steps have already 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: odin@bettyblocks.com