Skip to main content

Global Variables

Global Variables, as opposed to Flow Variables, are accessible by all Flows in a solution and have an unlimited validity, meaning that Global Variables can hold data that is persistent during the session and only expires when the session ends or times out.

This section covers the following topics:

Concept

Global Variables can hold data that is persistent during the session and only expires when the session ends or times out. They are accessible for reading and writing from all Flows and global documents, such as Listeners, Contexts or Integrations, as well as from most script events in your solution, making it possible to assign and pass on dynamic information throughout an entire session. In that way, they differ from Flow Variables, which only exist during the processing of a Flow, and will be forgotten as soon as a Flow is dropped from the flow stack.

In a general context, variables are placeholders for changeable values and can be of any type that Groovy (and Java) supports, for example strings, booleans, maps, Integers, lists, and custom objects.

info

A Global Variable lives for the entire session, or until it is reset, whereas a Flow Variable is forgotten as soon as a Flow is dropped from the flow stack.

Data Scope

Variable changes are automatically logged whenever the variable reference is changed during a transaction. This allows support for deeper debugging and reporting on specific types of sessions.

The process of logging this data, however, is automatic. Therefore, there are a number of things which should be considered in the solution design to prevent excessive or sensitive data from being automatically recorded, such as:

  • If the information is of sensitive nature, such as API keys, tokens, passwords etc., the best practice is to use Secrets instead.
  • When data is the same for all sessions and doesn't change over time, use a static property in a class defined in Solution Loaded script instead.
  • If the information is part of the dialog history, instead extract it only if and when it is actually needed.

Naming conventions

There are various naming conventions related to computer programming and variable names, for example, snake_case or camelCase, and it is common for a team to use a convention which makes the names easy to read and understand for everyone.

Within the Teneo Platform, the scripting language is Groovy, which makes use of camelCase; furthermore, Java describes the use of camel case in their Java Language Basics (see last bullet point).

For Teneo solutions, the following recommendation are the following:

  • Variable names must be unique within the solution.
  • Use lower camelCase.
  • Use a short yet meaningful name that communicates the purpose of its use to the reader.

Examples of valid variable names include user, userName, userFirstName, companyName, month, hour.

Lifespan

By default, the value of a Global Variable lives for the entire session, or until it is reset. You can, however, make a Global Variable forget its value after a specified number of turns. The variable will then be cleared and get back its default value. In Teneo this is called 'setting the lifespan' of a variable. A common use case is to remember something the user says in one Flow, so that it can be used in another Flow.

If you have a Global Variable called topic, you would set its lifespan like so:

_.setSessVarLifespan('topic', 2)

You would typically set the value and its lifespan at the end of a Flow. In this example, the lifespan is set to two turns, but you are free to choose whatever value you see fit.

The lifespan includes the request/input that is currently being processed. Each subsequent request (or input or turn) will consume one of the lifespans and when the lifespan reaches 0, topic will be reset to the value that was used when it was initialized.

note

Any Flow can overwrite the value of the Global Variable at any time and give it a new lifespan.

How To

Create

To create a Global Variable, follow these steps:

  1. Navigate to the Global Variables section in the backstage of Teneo Studio (Solution tab > Globals > Variables).
  2. Click Add next to the text filter.
  3. In the right side of the screen, write a Name and optionally a Description.
  4. Next, provide the Value of the variable.
  5. Click Save in the top to preserve the changes.

Edit

To edit a Global Variable, follow these steps:

  1. In the list of Global Variables, select the Variable to edit.
  2. Click Edit (right side of the screen above the Variable's summary).
  3. Now, update the Name, Description and/or Value as needed.
  4. Remember to Save the changes when done, or alternatively click Cancel to discard the changes and exit the edit mode.

Below please find an overview of more editing options available in the ribbon when working with Global Variables.

Save

When a Global Variable is in edit mode, the Save button becomes available in the ribbon above the details of the Variable itself; click Save to save with comment or alternatively press Alt and click Save to bypass the comment.

Close

In Teneo Studio Desktop, the view of a Global Variable in edit mode is closed by either clicking Cancel or Save to preserve any possible modifications.

Delete

To delete a Global Variable, follow these steps:

  1. Select the variable(s) to be deleted. Multi-select is available by pressing Ctrl while selecting.
  2. Click Delete in the top-area of the screen.
  3. Note that the right side of the screen will now display a Variable delete pending view, which the user should either Cancel or Apply for each of the selected variables for the actual deletion to be performed.
warning

If Teneo Studio detects that the Global Variable is in use, the user is prompted to confirm a Force delete or to Cancel the deletion.

Filter

A text filter is available at the top of the list of Global Variables. To filter on the available variable, simply write the desired letter combination to start filtering on the list.

UI

View of Global Variables

The Global Variables are available in the backstage of Teneo Studio by clicking the Solution tab in the main window of Teneo Studio, then Globals and lastly Variables.

global variable view

In this view, the user will find:

  • Left side:
    • Text filter
    • List of existing Global Variables
  • Right side:
    • Ribbon options; read more in the table below.
    • Details of the selected Variable providing a read-only summary
    • Global Variable's Id
ButtonDescriptionRelated pages
EditAllows to open the Global Variable in edit mode.
Search - Search by Variable NameAllows to search through all places a variable can be used when referenced by name, for example, in scripts, Output Answers, etc.
Search - Search by Variable ReferenceAllows to search through trigger/transition Match requirements and After Match actions, Flow Link and Integration transfer Variables
DeleteProvides the option to delete a Global Variable.Delete Global Variable
HistoryOpens the History of the selected Global Variable; previous versions and revisions of the Global Variable are listed, comments added at save are also displayed here. Select any of the revisions to view its content.Set a previous version as stable by selecting it in the list and clicking Set Stable, restore a previous version by selecting it and clicking Restore.Version Control
Set/unset stableSet the current version of the selected Global Variable as stable or unset it.Version Flags
Include/ExcludeInclude or Exclude the selected Global Variable from branching when working in a Localization setup.Localization setup

Global Variable Window

When adding or editing a Global Variable, a window will open on the right side:

global variable window

SectionDescriptionRelated pages
NameAdd a name for the variable.
DescriptionAdd a description for the variable.
Initial ValueSet initial value of the variable; set as "" if empty.
BranchingInclude or Exclude the selected Global Variable from branching when working in a Localization setup.Localization setup

Practical Examples

Share Information between Flows

Just as in human conversations, the user will assume your bot remembers what they recently have been talking about. Here you will learn how to make your bot remember information from one Flow to another.

Consider the following conversation:

User: Which coffee would you recommend? (Flow: User wants bot to suggest a coffee)

Bot: Easy choice, a flat white of course!

User: I'll go for one of those then. (Flow: User wants to order a coffee)

Bot: Ok, a flat white will be ready for pickup in 5 minutes.

This conversation combines two flows: 'User wants bot to suggest a coffee' and 'User wants to order a coffee'. In the first Flow, the bot recommends a 'flat white'. This coffee type is remembered. When the second Flow is triggered and the user asks for "one of those" the bot understands that a 'flat white' should be ordered. In other words, 'flat white' is mentioned in one Flow, and then used in another!

This logic can be applied to a variety of contexts.

To make sure a Global Variable works in two Flows, you should:

  • Create a Global Variable.
  • Make sure a Flow populates the Global Variable.
  • Make sure another Flow retrieves the Global Variable and triggers on inputs.

Create a Global Variable

First we will create the Global Variable to hold the variable:

  1. Create a Global Variable and give it a name. Assign it the value "". This Global Variable will be able to store any string value and be used by any Flows in the solution.
  2. Return to the main solution view.
  3. In the Tryout panel, a message to 'Reload now' appears. After clicking it, you will be able to use the Global Variable in your Flow.

We now have a Global Variable which we can use to store any string value and in which turn can be used by other Flows.

Assign Flow Variable to Global Variable

To assign a Flow Variable value to a Global Variable, we will add a script in the Flow's 'On Drop' script event. Scripts in 'On Drop' are executed when the Flow has finished and is about to be dropped from the Flow stack.

In the script, we will first assign the Global Variable the value of the Flow Variable. However, we do not want the Global Variable to be remembered forever. Just like humans do not keep things in focus forever, your bot should not either. Once the variable is set, we will use a special method to specify that the value should be forgotten after a couple of turns (input-output turns). In Teneo that is called setting the lifespan of a variable.

note

This example assumes you have a Flow Variable which you want to turn into a Global Variable.

Let's update the Flow:

  1. Open your Flow in edit mode.
  2. Click on the FLOW tab.
  3. Select Scripts, and then click On Drop.
  4. Populate the script field with the following lines and your Flow Variable name and Global Variable name.
    // Save flow variable in global variable
    globalVariableName = flowVariableName

    // Set the lifespan of the global variable
    _.setSessVarLifespan('globalVariableName',2)

  5. Hit Save and close the Flow.

Ensure Flow Retrieves the Global Variable

Now we will move on to retrieve and use the variable within a Flow. First, we need to make sure that the Flow triggers on inputs referring to the variable, such as "I'll go for one of those then" (see conversation above). The new trigger will therefore include a context restriction stating that the Global Variable must carry a value for the trigger to be applied.

Let's create the trigger:

  1. Open a Flow in edit mode.
  2. Click on the Plus icon next to the trigger and add a new Intent Trigger. This will add an additional trigger to the Flow.
  3. Name the trigger and add a few example intents.
  4. Add a Match for TLML Syntax and define the inputs that should be linked to the Global Variable. This could look something like the following syntax, where references like "one of those" or "it" are linked to the Global Variable coffeeTypeInFocus:
    (
    (%I.FW.LEX &^ %WANT.VB.LEX &^ (one/%IT.FW.LEX))
    /
    (%I_WILL.PHR &^ %GO_FOR.VB.MUL &^ %IT.FW.SYN)
    )^{ orderedCoffeeType = coffeeTypeInFocus }

  5. Now we will add the context restriction. Click on Add in the Match section.
  6. Select Global Variable and select the Global Variable that you created earlier.
  7. Hit Save.