In one of my previous posts about Copilot Studio, I’ve already had a look at Variables and how to work with variables. However it will be useful to take this a bit further. In this post I’m looking at all the things you can do with variables in Copilot Studio.

Types of variables in Copilot Studio

It is important to understand the structure of variables in Copilot Studio. The following types of variables are available in Copilot Studio.

  • Custom
  • System
  • Environment
Select a variable in Copilot
Select a variable in Copilot

Custom variables – are the variables that are created within your topic. You will find that Microsoft also refers to these as Topic Variables

System variables – are variables that supply details on the conversation. So for example you can figure out where the user is using your agent and who the user is.

Environment variables – Environment variables have been around for a while now and like in Power Automate where you can use environment variable to for example point at a SharePoint site or list, we can use the same environment variables within Copilot Studio.

Environment Variables in Copilot Studio
Environment Variables in Copilot Studio

Types of Variables in Copilot Studio

Yes, there is another categorisation we can look at when we look at variables. Variables can use the following data types:

Data TypeDescription
StringA text
BooleanA true or false value
NumberA number
TableA list of values or records. Notice that each records/value within a single table will have to use the same type
RecordA list of properties
DateTimeA Date and time
ChoiceA list of options
BlankA no value aka null

Now with the above types we can create any data structure that we may want to use in our agents.

Records and Table of Records

Most of the above datatypes are easy to use. However tables and records has always been more complicated.

In my post about actions in Copilot Studio I already mentioned an example of a table. I used a SharePoint action that returned a table.

Retrieving data from a connector action
Retrieving data from a connector action

In the above example I just displayed the table. And later on with the following expression I selected the DisplayName property of the first record.

First(Topic.ListRootFolder).DisplayName

Important functions (like we are used to in Power Automate and Power Apps) are:

  • First
  • FirstN
  • Last
  • LastN
  • Index
  • CountRows

But also functions like ForAll are available. Remember earlier we had a table of JSON code returned by that SharePoint action that gives us all the SharePoint lists.

If we now wanted a table with just the list names then this will work too:

ForAll(Topic.ListRootFolder,DisplayName)
Using the ForAll function in Copilot Studio
Using the ForAll function in Copilot Studio

Accessing variables in Copilot Studio

Topic, System and Global give us access to the various variables.

So when we type “Topic.” in the Power Fx editor, (By the way has anyone ever mentioned this small editor expression in the classic UI in Power Automate to anyone??) then the topic variables will be suggested by the intellisense.

Topic variables
Topic variables

For System Variables we can now see that we can get to details about the Activity, Bot (Agent), ClientPluginActions, Conversation, LastMessage, Recognizer and User.

System Variables in Copilot Studio
System Variables in Copilot Studio

For Global Variables we first may need to create a global variable. In the Variable properties, it is easy to switch between Topic and Global varibales.

Topic/Local and Global variables
Topic/Local and Global variables

Once you have created a global variable it can be found with “Global.” in the expression editor.

Global variables shown by intellisense
Global variables shown by intellisense

Thoughts about variables and querying Objects

One important thing to notice here is that Power Automate has two ways to access properties

Object.Property
Object?['Property']

Within Copilot only the first option is available. This means that a property will need to exist to avoid any errors. Or we might have to check for the existence of the property first before querying it.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.