Variables and Set


What are Variables?

Variables can be contextualized as containers that allow them to have data/information stored inside of it, and to be able to be used elsewhere or recalled at a later time. In Voiceflow, the importance of variables can be seen in this simple exercise.

Ex: Let's say you want to personalize your conversational assistant by the user's name. The below screenshot outlines one introductory method in order to do so:

The above example is not scalable, and it would not be feasible to remember every user's name manually. Furthermore, the above example would only work if your user's name was actually Joe, and this would not be dynamic (or personalized). For information you expect to get filled or can vary and there is a need to be contained, you will want to use variables.

Here is the above example improved, with variables:

Note: The below method replicates a use case of what the Set Step accomplishes, but the changes/variable declarations are only test-specific and unique for that specific design-test session and not in your assistant.

To do this, start with testing your experience by hitting the blue Run (shortcut R) button:

  • Navigate to Variables sidebar, when in Test mode under the Test Tool on Canvas
  • Set the value of your desired variable by inputting in the desired field in the Variables menu or setting your desired Variable State/Persona (testing with Variables).
  • You can then Run Test:

Tip: You can learn more about testing & prototyping with Variables and their states in this doc.

How do I create a variable?

You can create variables in your assistant in multiple different ways:

  • In any Talk Step type, start typing in the input field with the left (curly) brace '{' and hit Create
  • In the Condition Step, in any of the If (Add a condition) fields with the same method above
  • In the API Step, in any of the input fields with the same method above
  • In the Agent CMS under the Variables page

You can use the curly braces { } to create new variables, but also scroll through and invoke and add pre-existing or previously defined/created variables in your assistant

With any of the methods to create variables above, you can create variables explicitly (or plan for future use of variables) or on-the-fly and as needed with opening curly braces.

Tip: Variables are broken down as pieces (containers) of logic dedicated towards programming and conversation design, so the variables must contain no-spaces

The next sections cover some options and steps available in Voiceflow in order to manipulate/alter, perform functions/expressions and end-to-end commands with Variables.

Note: Though behaving similarly, Variables and Entities in Voiceflow are commonly confused. Learn more about how each data/logic type operates in this doc here.


How do I force assign or set values of variables to my assistant?

Often times in your conversation, you will want to alter variable values if a user reaches a certain point in your conversation or performs a certain action. Sometimes, you will want to reset values or alter them based on circumstance.

In Voiceflow, these specific instances where you want to 'forcefully' (manually) change or set a value of variable(s) in your conversation, requires usage of the Set Step. The Set Step allows you to set and change the value of variables.

Setting and Changing Variable Values

The Set step lets you change the value of a variable when the Set Step is activated. You can get started by dragging the Set step, from the Logic section in your sidebar. Don’t forget to assign a label to your set.

Tip: By leveraging Add Set in the Set Step menu, you can add and also change multiple variable values at once and the changes will take place from top-to-bottom. You can do this with the Add Set (3) button.

Next, you will want to choose a variable in the Apply To (2) input.

You can now set the value, variable ({ }) or expression you want to your variable in the previous step, by entering it into the Value Type section in the manner you want the variable to adopt.

Note:You can also set your variable to a specific text-value (String-value) but you will be unable to perform expressions or equations, as indicated in the next sections.

Setting Expressions — Adding and Subtracting

A common way to change a variable value is to increment it. To increment a variable means to increase it by the same amount at each change.

Ex: A conversation designer may increment a scoring variable by +2 each time a basketball goal is made. Decreasing a variable in this way is known as decrementing the variable value. A conversation designer may decrement a point total by –1 each time a user responds to a question incorrectly.

In the Set Step menu, you can also toggle to configure the Value Type to be a Variable or Expression. You can use the Expression field to increment or decrement variable values using the Set step.

To do this, select the variable you want to increment and then create the equation you want the variable to be equal to.

Ex: To increment a variable value by 5, select the variable you want to increment and set the expression to {variable} = {variable} + 5

Setting Expressions — Complex Equations

The Expression field also unlocks the possibility to evaluate complicated expressions, by allowing you to write JavaScript statements.

Tip: You can track how your variables are interacting in your conversation (and changing based on Set Steps) by turning on Debug Mode (Settings after you hit Run) while attempting to test your conversation on-Canvas