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.
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 want to 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:
Variables:Key | Prefix | First name |
Last name |
Input | 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:
VariablesKey | Number 1 | Number 2 |
Input | 3 | 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 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 | first_name | last_name_prefix | last_name |
Input | John William | De | Graaf |
Result (output type: Text):
John William 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 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
"<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”
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 added in brackets ().
Input
Value:
-
function formatPhoneNumber() { // Return empty text if there is no phone number if (!"{{ office_phone }}") return ""; // Only get numbers from text const onlyNumbers = "{{ office_phone }}".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 | office_phone |
Input | 1234567890 |
Output:
-
Output type: Text
-
Value: “(123) 456-7890”
Monday of week
Retrieve the date of a Monday from a week and year you provide.
Input
Value:
function getMondayOfWeek() { const date = new Date({{ year }}, 0, (1 + ({{ week_number }} - 1) * 7)); const offset = date.getTimezoneOffset(); return new Date(date.getTime() - (offset * 60 * 1000)).toISOString().split('T')[0]; }();
Variables
Key | year | week_number |
Input | 2024 | 25 |
Output:
-
Output type: Text
-
Value: “2024-06-17”
This value can be saved in a date field and be used further in your application.
Get array element
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.
Input:
Value:
“{{ array }}“.split(“,”)[{{ index }}]
Variables:
Key | array | index |
Input |
[“Element 1", “Element 2”, “Element 3"] |
1 |
Output:
-
Output type: Text
-
Value: “Element 2”
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.
Input:
Value:
[{{ collection.map(item => item.id) }}].filter(item => item !== {{ record_id }})
Variables:
Key | collection | record_id |
Input |
[{ id: 1, title: “Element 1” }, { id: 2, title: “Element 2" }, { id: 3, title: “Element 3” }] |
2 |
Output:
-
Output type: Array
-
Value: [{ id: 1, title: “Element 1" }, { id: 3, title: “Element 3” }]
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