Work with global variables
Variables store your customers' responses to questions from your agent. For example, you can save a customer's name in a variable called UserName
. The agent can then address the customer by name as the conversation continues.
By default, a variable's value can only be used in the topic where the variable is created. However, it's possible to reuse the same value across topics. For example, a Welcome topic asks for the customer's name and email address. In the Appointment Booking topic, you want the agent to remember what the customer entered and not ask again.
One way to reuse a variable is to pass the variable from one topic to another. The other way is to make the variable global in scope, and that's what this article covers. Global variables are called that because they're available in all topics across the entire agent. They can also be set from external sources.
Global variables apply during a single user session. You specify which variables are global variables to distinguish them from topic-level variables.
Create a global variable
You create a global variable by changing the scope of a topic variable.
Note
The name of a global variable must be unique across all topics.
Create a variable or use the Variables panel to open an existing variable.
On the Variable properties panel, select Global (any topic can access).
The variable name is given the prefix
Global.
to differentiate it from topic-level variables. For example, the variableUserName
is displayed asGlobal.UserName
.Save the topic.
Use global variables
When you're composing a message in a Message node or a Question node, select the {x} icon to view the variables that are available to the topic. Global variables appear in the Custom tab along with any topic variables. Variables are listed in alphabetical order.
Find all topics using a global variable
You can find where a global variable is defined and what other topics are using it. This feature can be useful if you're working on a new agent, or if you have multiple variables and complex topic branching.
Select the desired global variable on the authoring canvas, or in the Variables panel.
On the Variable properties panel, in the Reference section, select View all references.
Switch to the Other tab, and select any topic where the variable is used to go directly to that topic and node.
Deleting global variables
If you remove a global variable used in other topics, the references to that variable in the topics are marked as Unknown
. You receive a warning about deleting the global variable before you can confirm the operation.
Nodes that contain references to a deleted global variable indicate that they contain an unknown variable.
Topics with nodes that contain references to deleted global variables might stop working. Ensure that you remove or correct all the topics that were using the deleted variable before publishing your agent.
Lifecycle of global variables
By default, the value of a global variable persists until the session ends. The Clear variable values node resets the values of global variables and is used in the Reset Conversation system topic. When a redirection triggers that topic (or when the user enters a phrase such as "Start over"), all global variables are reset.
Set global variables from external sources
To make sure the agent starts a conversation with some context, you can use a global variable and set its value from an external source. Let's say that your site requires users to sign in. If you store a user's name in a global variable and pass it to your agent, the agent can greet customers by name before they start typing their first question. Another example scenario is passing context from Dynamics 365 Customer Service to an agent so it can start the conversation with knowledge of what the customer wants to achieve.
To prevent undesirable latency, you can specify how long your agent can wait for a value. You can also set a default value to use when the external source fails to respond in a timely fashion.
Note
Agents that are published to the Dynamics 365 Contact Center channel for IVR use cases don't support time-out values configured for global variables set by external sources.
Create a dedicated topic to hold the configuration for all the variables meant to be set from external sources. You might name this topic "Set context variables" for example. This topic doesn't serve any other purposes, so it doesn't need to have trigger phrases set.
Add a Set variable value node to your dedicated topic.
Under Set variable, open the variable picker, and select Create a new variable.
Select the default name of the new variable. The Variable properties panel appears.
Replace the default name to one that matches exactly the name of the variable being passed in from the external system.
Under Usage, select Global (any topic can access), and External sources can set values.
Under Reference, select the three dots (⋮) in the upper-right corner, and select Get value from this node if empty.
(Optional) Set a time-out delay, in milliseconds. This value determines how long your agent can wait for the variable to be set by an external source before timing out and continuing with the default value you set in the Set variable value node. This setting is relevant in scenarios where the variable depends on a long-running or asynchronous process, but your agent must respect a maximum latency to ensure a good user experience.
For variables coming from Omnichannel for Customer Service, we suggest a value of 10 seconds (10,000 ms) as a maximum wait time.
In the Set variable value node, enter the default value to use if the time-out is reached. At runtime, your agent will expect values with the same data type. If you want this default value to be an empty string, use
Text("")
as a formula.For any other values you expect to come from an external system, add more Set variable value nodes to your dedicated topic, and configure the required global variables in the same fashion.
Thus configured, your agent is ready to test. When the agent is invoked, instead of waiting indefinitely for all variables to be populated, your agent can immediately start sending any messages that aren't dependent on the variables being passed in. When your agent attempts to access a variable that's being set externally, it pauses until the value arrives or the time-out occurs. Learn more about optimizing agents to minimize latency.
Important
During the normal flow of a conversation, if your agent sets a variable that would otherwise come for an external value, the value set from within your agent prevails. Any value passed in context is ignored. This rule prevents your agent from overwriting values intentionally set in topics.
Set global variables in an embedded agent
If you're embedding your agent in a simple web page, you can append variables and their definitions to the agent's URL. Or, if you'd like a little more control, you can use a <script>
code block to call and use variables programmatically.
The variable name in the query string of the URL must match the name of the global variable without the Global.
prefix. For example, a global variable Global.UserName
would be referred to as UserName
in the query.
The examples that follow uses a basic declaration for the variables. In a production scenario, you might pass in as the query parameter or variable definition another variable that already stores the user's name (for example, if you have the user name from a sign-in script).
Append the variables and their definitions to the agent's URL as query string parameters in the format botURL?variableName1=variableDefinition1&variableName2=variableDefinition2
.
For example:
- You have a global variable named
Global.UserName
. - Your agent's URL is https://web.powerva.microsoft.com/webchat/bots/12345.
- To pass in the user's name when starting an agent conversation on your website, attach the
UserName=
query string as: https://web.powerva.microsoft.com/webchat/bots/12345?UserName=Ana.
The parameter name is case-insensitive. username=Ana
would also work in this example.
Add global variables to a custom canvas
You can also add the variable to a custom canvas.
In the
<script>
section on the page where you have your agent, define the variables as follows, substitutingvariableName1
for the variable name without theGlobal.
prefix andvariableDefinition1
for the definition. Separate multiple variables with commas (,
).const store = WebChat.createStore({}, ({ dispatch }) => next => action => { if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') { dispatch({ type: "WEB_CHAT/SEND_EVENT", payload: { name: "pvaSetContext", value: { "variableName1": "variableDefinition1", "variableName2": "variableDefinition2" } }, }); } return next(action); });
In your
<script>
section, call thestore
when you embed your agent, as in the following example wherestore
is called just before wherestyleOptions
is called (you must replace theBOT_ID
with your agent's ID):const BOT_ID = "12345-5678"; const theURL = "https://powerva.microsoft.com/api/botmanagement/v1/directline/directlinetoken?botId=" + BOT_ID; fetch(theURL) .then(response => response.json()) .then(conversationInfo => { window.WebChat.renderWebChat( { directLine: window.WebChat.createDirectLine({ token: conversationInfo.token, }), store, styleOptions }, document.getElementById('webchat') ); }) .catch(err => console.error("An error occurred: " + err));
Authentication-related global variables
Depending on the agent's authentication setup, you have a set of global variables associated with the selected authentication provider. For details about which set of variables are available and how to use them, see Add user authentication to topics.