Topics Map > Actions
LCS - Basic Expressions
- Text (Strings)
- Numbers
- Combining Text and Numbers (Concatenation)
- Current Date
- Adding time to a Date
- The Ternary Operator
- Comparisons (Less Than and Greater Than)
- Using expressions in an action step
Expressions can be used to combine text, add numbers, work with dates, and more. What that means is that if you want to take two pieces of text like "first name" and "last name" and combine them, you can make an expression that looks like "first name" + " " + "last name". Note that " " has a space between the quotes. You can also add numbers and combine that with a text. You can add time to dates. For example, you can take the current date and add 2 weeks to it. Text, numbers, and dates are often in variables.
This is just the beginning. You can create complex expressions with many variables and use the result of that expression elsewhere in your action.
Expressions work much like Javascript. If you know Javascript it can help you understand expressions.
We will cover some basic expressions in this document. A longer list of expressions can be found in this Betty Blocks Expression step reference article. You can look at that as a reference when creating expressions.
To use expressions, you will need to drag the expression function into your action and configure it. The expressions will be entered in the EXPRESSION box as in the image below. How to set this up and add expressions to your action will be covered at the end of the document.
Text (Strings)
If you have two variables called first_name and last_name, you can combine them using an expression syntax like the following:
"{{first_name}}" + " " + "{{last_name}}"
Anytime you want to reference a variable such as first_name you need to wrap it in "{{ variable }}" so the result is "{{first_name}}". If first_name was set to "Jane" and the last_name was set to "Smith" then resulting expression would be "Jane Smith". It is important to use the quotes ("") to indicate that it is text. Otherwise, it is assumed they are numbers.
Numbers
If you have a variable called item_cost_x that has the value of 5 and another variable called item_cost_y you can add them together. The expression would look like the following:
{{item_cost_x}} + {{item_cost_y}}
You should NOT use quotes ("") around the variable like you do with text. When you omit the quotes, the variables are assumed to be numeric. Note that when you add text it combines them into longer text. When you add numbers, it adds them mathematically.
You can add (+), subtract (-), multiply (*), and divide (/) numbers using those symbols.
Combining Text and Numbers (Concatenation)
If you want to add two numbers and combine that result with text, then you need an expression like the following. In this example, number_x is 5 and number_y is 2:
"I am number" + ( {{number_x}} + {{number_y}} )
The result would be:
"I am number 7".
In that case you need to put the parenthesis around the number variables, so it adds the numbers together first and then combines it with the text. Otherwise, it would combine the numbers like text. If you did not use the parenthesis then the result of the previous expression would be, "I am number 52" which might not be what you want.
Current Date
You can get the current date with the following expression:
new Date().toISOString().substring(0,10)
That looks a bit confusing, but it is saying get a brand-new date which is the current date and time. Then convert it to a standard format (ISO). Then only take the first 10 characters of it which is the actual date. It cuts the time off at the end of it.
Adding time to a Date
To add time to a date, use the Date/Time Offset block provided by Betty Blocks. You can download it from the block store and then you can use it in your actions as an action step.
After you have downloaded the Date Time Offset block you can access it as an action step in your actions.
Drag the Date Time Offset function onto your action and select it to bring up the sidebar. You can select the date to use by any of these four methods:
- Specify Your Date by typing it in.
- Select the (x) to select one or your existing properties or variables like the image below.
- Select the checkbox for “Use the Current Date/Time.”
- Create a variable to use for this action step.
Set the offset type from the dropdown list. In this example we are selecting, “weeks.” Then specify what the offset is. In this case It is 2 for 2 weeks. Finally specify the result type. You can pick Date, but there are other options for date time, time, and Unix time stamp.
The last step is to set the UTC offset. For central time in the U.S., it is UTC-6. Give a name for the RESULT so you can use the result of this expression elsewhere in your action. Now you can save your Date Time Offset and you are done.
The Ternary Operator
If you want to make a choice depending on what a variable is, you can use the ternary operator. It works like a condition action step. It is an if…then…else statement. It looks like the following:
"{{user_object}}" ? true: false
In that case, it is saying, "Does the user_object variable exist? If it does, then the result is true. If it does not, then the result is false."
Another way to put it is that you write down a statement. Then put a question mark to ask if the statement is true or not. If it is true, then you get the first result. Otherwise, you get the result after the colon (:).
Statement ? result if statement is true : result if statement is false
These will be used more often for advanced expressions and there will be more concrete examples there.
Comparisons (Less Than and Greater Than)
You can compare numbers using the less than (<) and greater than (>) symbols, i.e., < and >. You can also use "less than or equal to" or "greater than or equal to", i.e., <= and >=. These can be used with the ternary operator which was covered previously. The way this works in the ternary operator is to check if the statement is greater or less than a value.
total1 >= total2 ? "total1 is greater than or equal to" : "total 2 is greater"
Note that both total1 and total2 in this example must exist and must be numbers. Otherwise, this will cause an error in the action.
Using expressions in an action step
To use expressions in your actions you will need to download the expressions action step from the betty blocks.
- Make sure you have downloaded the Expressions block from the Block Store.
- Go to your action and drag the expression action step onto your action.
- Select it to open the Expression sidebar. Note that the expression in there is just an example. You will need to type in your own expression.
- Write the expression that you want. In this case we are just overwriting the expression that was in there to add the first and last name together.
In addition, you will need to use the +Add button to add in the two variables needed for the expression. Give each one a name. In this case it is first_name and last_name. Then assign them a value. You can type it in or use the (x) to get it from another variable or model in your action like we are doing here. You must add the variables in here or you will not be able to use them in the expression. - You need to specify whether the result of your expression is a number, text, or Boolean. A Boolean is true/false. You also need to give it a name to save the result of the expression. Then you can use it elsewhere in your action. Just use the Save button to save your expression and you are done.