Builder blocks
Code Set block
12 min
this block is only available for web chatbots the code set block, allows to set up the value of a landbot variable using javascript it avoids the need to use a user input (buttons, text question ) to be able to use immediately the new value created 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, that are numbers 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 with 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 examples 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 = 'itemprice'; for (let i = 0; i < myarray length; i++) { let row = \<tr> \<td>${myarray\[i]\['item']}\</td> \<td>${myarray\[i]\['price']}\</td> 	 \</tr>; table = table + row; } table += ''; return table; check day of the week and return if business is open or closed 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;