Set variable
The “Set variable” block allows you to set a particular value to a variable.
Custom
You can set your variable with any value with Custom
. It can be any kind of plain text but also Javascript code.
Expressions with existing variables
It means you can apply operations on existing variables.
Add a value to your variable:
Compute a sum of variables:
Multiply variables together:
Compute a percentage:
Extract the first name from a full name:
Transform existing variable to upper case or lower case:
This can also be Javascript code. It will read the returned value of the code and set it to your variable.
If you don’t provide the return
keyword then it will be automatically prepended to the beginning of your code.
is the same as
Variables in script are not parsed, they are evaluated. So it should be treated as if it were real Javascript variables.
So, if you write "{{My variable}}"
, it will parse the variable ID (something like vclfqgqkdf000008mh3r6xakty
). You need to remove the double quotes to properly get the variable content value.
For example,
- ❌
"{{URL base}}/path"
=>vclfqgqkdf000008mh3r6xakty/path
- ✅
{{URL base}} + '/path'
=>https://domain.com/path
- ✅
`${{{URL base}}}/path`
=>https://domain.com/path
Variables content can either be a string or a list of strings. Check out Valid value types for more information.
Empty
Resets your variable as if it was never initialized.
Append value(s)
A conveniant value that automatically transform your variable into a list of strings. It will append the value(s) to the list.
3 possible cases here:
- If the variable is empty, it will create a new array with the provided value(s)
- If the variable is not an array, it will create a new array with the existing value followed by the provided value(s).
- If the variable is an array, it will concatenate the provided value(s) to the existing array.
Environment name
This will set your variable with either web
or whatsapp
depending on the environment.
Transcript
This preset value will save the entire conversation transcript in a variable. It is super useful to provide context to an AI block or to send it as a recap with the Send email block.
Result ID
This will set your variable with the current result ID. The result ID is the ID that corresponds to a row of your Results table. It can be considered like a User ID for the currently chatting user.
Yesterday, Now, Tomorrow
This will set your variable with the specified date and time in ISO format. You can optionally provide a time zone to convert the date to the specified time zone.
Random ID
This will set your variable with a random ID with the CUID algorithm.
Moment of the day
It will set your variable with either one of these values based on the user’s time of the day: morning
, afternoon
, evening
, night
.
Then you can use this variable to conditionally display content:
Map item with same index
This is a convenient value block that allows you to easily get an item from a list that has the same index as an item from another list.
When you are pulling data from another service, sometimes, you will have 2 lists: Labels
and Ids
. Labels are the data displayed to the user and Ids are the data used for other requests to that external service.
This value block allows you to find the Id
from Ids
with the same index as Label
in Labels
Phone number
Only available in WhatsApp. This will set your variable with the user’s phone number.
Contact name
Only available in WhatsApp. This will set your variable with the user’s name.
Pop / Shift
Pop removes the last item from the list variable that you provide and it will set the removed item into the “Popped item” variable. Shift does the same with the first item of the list. These are especially useful when you need to create a loop that processes each item of a list.
Save in results
By default, new variables are not persisted in the Results table. They are only stored for the current user chat session. Enabling this option will save the variable in the Results
table.
Execute on client
This option is useful when you want to execute the custom code on the client side. This is only necessary when you need access the user’s browser information. So, if you need access to window
, document
, navigator
, etc., you should enable this option.
Get user’s geo location
For this you can provide the following custom code:
This custom function can only work when it is executed on the client browser so you need to make sure to enable the “Execute on client” option.