Copilot Studio Standards: Variable Scopes

In Copilot-Studio, variables are playing the key role for automating the conversations, enabling agents to store, manipulate and share data efficiently. Understanding the difference between global and local variables is key to designing topics that are both scalable and maintainable.

Introduction

When we are developing agents in Copilot Studio, variables will always play the main part. They are like a glue that holds everything together, making sure conversation bot doesn’t break in the middle. Whether you are tracking user data, passing information between actions, or pulling information from APIs, Variables are the heart of it all.

Variable Scopes

The scope of a variable determines where it can be used in an agent application.
  • If the variable required to be used in multiple topics, use a global variable.
  • If the variable required to be within a single topic, use a local variable.
  • Some variables are automatically generated during the creation of a new agent called System Variables.
  • If the variable required to be accessed or used across multiple applications in Power Platform, use environment variables.
Variable Type How to Access Scope Lifespan
Global Global.variablename Variable is available across all topics Entire agent life cycle
Local Topic.variablename Variable is available on a single topic Topic execution life cycle
System System.variablename Variable is available across all topics.
Variables related to contextual information about Conversation and User.
Entire agent life cycle
Environment Env.variablename
Variables are available across all applications in Power Platform.
Read-Only in Copilot-Studio
Across applications. No limitation to access the value.

Examples of Variable Scopes

Global Variable

Global variables are updated based on Set Variable action in a Topic and, we can use other actions output to set value to Global variable.

Local Variable

Local variables are also updated based on Set Variable action in a Topic and, we can use other actions output to set value to local or topic variable.

System Variable

System Variables are auto generated during the creation of an agent in Copilot Studio. Accessible across multiple topics. We can use Set Variable action and other actions output to update the value to this variable.

Environment Variable:

We can’t set the value to environment variable from Copilot Studio. Agent allows us to use the variable across multiple topics in Copilot Studio.

When to use Global Variable?

  • Sharing values across topics: When the same data needs to be accessed or updated across multiple topics.
    ex: Share the user authentication status across topics. gisUserAuthenticated
  • Constant values across sessions: For storing values that need to remain accessible across the entire life cycle of a conversation session.
    ex: Global configuration to specify how many responses to returned. gMaxReturns
  • Reusable values: When multiple topics or actions share common values that don’t change frequently.
    ex: Global list of product categories gProductCategories
  • Debugging and Logging: To log or monitor agent-wide activity for easier debugging
    ex: Logging errors gErrorLog

When to use Local Variable?

  • Topic specific data: When data is only relevant to single topic and doesn’t need to be shared.
    ex: lOrderStatus – Track the status of the order within Topic
  • Temporary data processing: When variables are needed to perform immediate calculations or handle short-lived data.
    ex: lParseData – Parse json of the retrieved value
  • Action specific tasks: When a variable is only used within the context of single action to store the output.
    ex: lResponseAnswer – Store the response from a generate answer
  • Avoiding overwrites: When variables need to stay alone from other topics to prevent unintended overwrites or conflicts.
Shantha Kumar
Shantha Kumar
Articles: 301