Table of Contents

How to use the Formulas block: a beginner's guide

The Formulas block works out a value with a function, such as an order total, a tidied-up answer or a yes-or-no check, and saves the result in a Field without asking the visitor anything. It works like a spreadsheet formula: you write Sum(2, 3) instead of =2+3, and every block after it can use the result.

The Formulas block is available on the Professional and Business plans.

Prefer watching? The Formulas video tutorial in the Landbot Academy builds formulas step by step.

When should you use the Formulas block?

Use the Formulas block when the next step depends on a value you have to calculate or clean up first: a price times a quantity, a callback time two hours from now, an answer turned into lowercase so it matches what you expect. Because the result goes into a Field, you can show it in a message, check it later or send it to another tool.

For a single sum or subtraction, a Set a Field block is enough, as Set a field calculations: add, subtract, multiply and divide numbers shows. To check an answer the visitor already gave, without changing it, use a Conditions block. Both blocks, and when to pick each one, are part of Flow logic: how your agent decides what comes next.

How to add a Formulas block

To add a Formulas block, click the output of the block that comes before it and pick Logic > Formulas, or type Formulas in Search by name.

Add-block menu with the Logic category open and Formulas listed between Global keywords and Jump to

Then set the block up in its panel:

1️⃣ In 1. Output, pick the Field where the result is saved, or type a new name and click CREATE.

2️⃣ Under Block outputs, pick SUCCESS / ERROR, TRUE / FALSE or CUSTOM, depending on what should happen next.

3️⃣ In 2. Formula, write the formula.

4️⃣ Click APPLY.

A Formulas block can't be the first block after the Starting point: that one has to ask the visitor something, so the Formulas block goes after it.

How to choose the Field for the result

Choose a Field whose format matches the result, because the format decides how the result looks when your agent shows it. When you click CREATE in 1. Output, pick one of five formats: TEXT, NUMBER, DATE, ARRAY or DATETIME.

The same result can look different in each format. For example, 4 multiplied by 12.5 shows:

● 50 in a NUMBER Field

● 50.0 in a TEXT Field

Save numbers in a NUMBER Field, dates in a DATE Field, and a date with a time, such as a booking or a callback, in a DATETIME Field. A formula that answers true or false goes into a TEXT Field, since there's no true-or-false format. The Fields article, Managing Data in Your Agent: A Guide to Using Fields, explains every format.

Which block outputs should you choose?

The block outputs decide where the visitor goes after the formula runs, so pick them by what you want to do with the result: SUCCESS / ERROR for calculations, TRUE / FALSE for checks and CUSTOM for results with several possible values. The examples below come from one pizzeria agent, Forno Rosa, which asks "How many pizzas would you like?" with an Ask for a number block and saves the answer in @pizzas.

Success / Error: when the formula calculates something

Pick SUCCESS / ERROR to give a calculation one path when it works and another when it fails, for example when the formula expects a number and gets text. In the Forno Rosa agent, a Formulas block multiplies @pizzas by the price of a pizza with Product(@pizzas, 12.5) and saves the total in @total, a NUMBER Field:

● SUCCESS, the total is worked out: the agent goes on to the free delivery check.

● ERROR, the formula fails: "Sorry, I couldn't work out your total. Let's try that again.", and the question comes back.

Formulas block saving Product of pizzas and 12.5 in total, with the success output to the next check and the error output to an apology

The live result under the formula, =50, comes from a test value of 4 for @pizzas, set in Test values.

True / False: when the formula checks something

Pick TRUE / FALSE to send the visitor down one path when the formula's answer is true and another when it's false, without adding a Conditions block. In the Forno Rosa agent, a second Formulas block checks whether @total is 40 or more with IsGreaterOrEqual(@total, 40) and saves the answer in @free_delivery:

● True, 4 pizzas (50 €): "Your total is 50 €. Delivery is on us! 🎉"

● False, 3 pizzas (37.5 €): "Your total is 37.5 €, plus 2.50 € delivery. Orders from 40 € get free delivery."

Formulas block checking if total is 40 or more, with the True output to the free delivery message and False to the delivery fee message

@free_delivery saves the answer with a capital letter (True), even though the panel's live result shows it in lowercase. A later formula has to use the same capital: IsEqual(@free_delivery, "True") is true, while IsEqual(@free_delivery, "true") is false.

Custom: when the result can have several values

Pick CUSTOM to give each value you name its own path, plus Any of the above for every other value. In the Forno Rosa agent, an Ask a question block asks for a promo code and saves it in @promo_code. A third Formulas block turns the code into lowercase with Lower(@promo_code), saves it back in @promo_code and compares it with two custom outputs:

● slice10, the visitor typed SLICE10, Slice10 or slice10: "SLICE10 applied: you get 10% off this order."

● freedrink, the visitor typed FreeDrink or any other spelling of it: "FREEDRINK applied: a free drink comes with your pizzas. 🥤"

● Any of the above, any other answer, such as pineapple: "No valid code this time, no problem."

Formulas block with custom outputs slice10 and freedrink matching the lowercase promo code, and Any of the above for other codes

Writing the custom outputs in lowercase and turning the visitor's answer into lowercase first means visitors can type the code in any capitals. To add an output, click Add another output.

How to write a formula

To write a formula, type the function's name and then its values in brackets, separated by commas: Product(@pizzas, 12.5). Each value can be:

● A fixed value: a number as it is (12.5), or text in double quotes ("True").

● A Field: @ and the Field's name, without quotes (@pizzas).

● Another formula: Sum(2, Sum(4, 2)) gives 8. Each formula inside is worked out first.

● A system Field: written the way the Formulas browser inserts it. Today's date is &date[today], so AddDays(&date[today], 1) gives tomorrow's date.

The line under the formula shows the result as you type, so you can check a formula before you test the agent. To try it with your own values, open Test values and type a value next to each Field.

When the formula is valid, the tick next to 2. Formula turns blue. A grey tick and =Error mean the panel can't read the formula. Three mistakes cause most errors:

● Wrong capitals in the function name. Function names are case sensitive: Sum(2, 3) works, SUM(2, 3) doesn't.

● A Field that doesn't exist yet. Create the Field first, for example in the block that saves the answer, and then use it in the formula.

● Quotes around a Field. "@pizzas" is read as text, not as the Field, so the formula fails in the chat.

A blue tick only means the panel can read the formula, not that the result is right. Text without quotes, such as Concat(Hi, there), gets a blue tick but gives an empty result in the chat, so always check the result line too.

To find the function you need, open a category in the browser under the formula. Clicking a function shows what it does, what it takes and a few examples:

Formulas browser with the Math category open and the help for the Round function with four examples

Several small Formulas blocks in a row are easier to read and fix than one long formula: in the Forno Rosa agent, the free delivery check reads @total from the block that works out the total.

How to find out why a formula failed

To find out why a formula failed, click See Logs at the top of the Formulas panel, or go to Integrations > Formulas > Error log. The Error log lists every formula that failed in your agents' conversations, with the agent's name and ID, the date and the error.

Formulas Error log filtered by one agent ID, listing failed formulas with their date and error message

To see only one agent's errors, type its ID in the search box, pick the dates and click APPLY. The agent ID is the number in the builder's address, after /bot/. The error tells you what went wrong: "Invalid cast to float for value abc" means a formula that expects a number got the text abc.

Formulas run when a visitor reaches them, so test the published agent with a few different answers, as How to test your agent explains. To see a Field's value at any point, show it in a message, as How to debug your agent's flow without code describes.

Which formulas can you use?

The Formulas block groups its functions by the kind of value they work on, in the same order as the browser in the panel. Each group has its own article with an example for every function:

● Comparison formulas compare two values, including two Fields: IsEqual, IsNotEqual, IsGreater, IsGreaterOrEqual, IsLess, IsLessOrEqual, IsSet.

● Logical formulas combine checks or choose between two values: And, If, Not, Or.

● Date formulas work with dates and times: AddMinutes, AddHours, AddDays, AddMonths, AddYears, Now, Today, ToDate, ToDateTime.

● Math formulas calculate with numbers: Power, Product, Quotient, Random, Remainder, Round, Subtract, Sum, ToFloat, ToInteger, Trunc.

● String formulas change and clean up text: Capitalize, Concat, Contains, Encode, Length, Lower, Replace, Slice, Split, Title, ToString, Upper.

● Array formulas work with lists: Contains, GetValue, IndexOf, Length, List, Slice, Splice, Push.

● Object formulas read values from structured data, such as an API response: GetValue, ToJSON.

● Regex formulas check whether text follows a pattern, such as a postal code: RegexTest.

Was this article helpful?

Set a field calculations: add, subtract, multiply and divide numbers

Regex formulas: check that an answer follows a pattern