Table of Contents

Code Set

Pau Sanchez Updated by Pau Sanchez

This block is only available for web chatbots

With the Code Set block you will be able to set up the value of a Landbot variable by using Javascript. This means you will be able to use a new value straight away, without having to use user inputs such as buttons, text, questions, etc.Β 

Landbot makes using Javascript extremely easy even for no-coders! By creating a Code Set block within your chatbot, all you’ll have to do is type or paste your Javascript code for the function you wish to add to your bot and we’ll do the rest.

Use the Code Set block to add Javascript to your chatbot to make calculations for your eCommerce website, add tables with information, tell your customers if your store is open and much more.

How to use it

Select Landbot variable to set

First of all we need to select (or create) the Landbot variable that we want to set using the Code set bloc

Use Landbot variable

If we are using values collected previously we need to set Javascript variables with those values:

In this example, we are using @value1 and @value2, which are numbers. If your values are strings, you need to set the variables with double quotes:

Returning value

Finally, need to return the value we want to set:

Always we need to end with the "return" action.

Here is how it would look like this example in the builder:

And the chat:

Actions to avoid

- Do not use to delay the bot using setTimeout

- Do not try to set up more than one variable at once

- If you are working strings that contain line breaks or objects that might contain boolean values (true false) use template literals to set Javascript variables

Example: let object = `@{array}`

- If a Landbot variable is not set, the block will fail and the bot will stop

- Do not return directly Landbot variables or operate like in the example below:


Below are a number of examples of how it can be used:

Simple Math operation

Get current year

Generate HTML for a Table

let myArray = @array;
let table = '<table><tr><th>Item</th><th>Price</th></tr>';
for (let i = 0; i < myArray.length; i++) {
let row = `<tr>
table = table + row;
table += '</table>';
return table;

Check day of the week and return if business is open or closed

let status;
switch (new Date().getDay()) {
case 0:
status = "Closed";
case 1:
status = "Open";
case 2:
status = "Open";
case 3:
status = "Open";
case 4:
status = "Open";
case 5:
status = "Open";
case 6:
status = "Closed";
return status;

Replace True, False, null values from an array of objects

let JsonStr = "@{array}";

let JsonStrReplaced = JsonStr.replaceAll('None','"None"').replaceAll('True','"true"').replaceAll('False','"false"').replaceAll("'",'"');
let cleanArray = JSON.parse(JsonStrReplaced);

return cleanArray

How did we do?

Code block