Environment Variables

This is a topic I’ve been meaning to write about for quite a while and fits quite nicely into the wider topic of application lifecycle management! You might’ve seen me reference environment variables many times in previous posts, but I could still be leaving… READ MORE [https://lewisdoes.dev/blo
Environment Variables
exc-62cb27f819728c4aaecd44ef
In: Low Code Lewis Content 🚀

This is a topic I’ve been meaning to write about for quite a while and fits quite nicely into the wider topic of application lifecycle management! You might’ve seen me reference environment variables many times in previous posts, but I could still be leaving you wonder, what are they, and how do they make my Power Platform solutions better? In this post, I’m going to discuss a bit about environment variables and why you should be using them in your solutions, especially if you’re implementing ALM practices (which by the way, you should be…) !

What are they?

So, the main question… what are environment variables? They’re quite literally what they say on the tin! They’re similar to the variables you might define in a Canvas App say, except they’re on an environment level as opposed to say an app or screen level… this means we can reference them in any of our Power Apps or Power Automate flows!

Keeping them unmanaged

Now, when we think about ALM and moving our solutions from development into source control and then our target environments such as test, UAT, and production, we’d want to always move our solutions into these target environments as managed.

Whilst this is the case for our main solution file, our environment variables are a little different. Environment variables have values defined within them that we’re going to want to change per environment, that’s sort of the point of them a little bit! So when it comes to our solution development, we’re going to want to package our environment variables in an additional solution to our main solution, and we will want to export and import this solution to our target environments as unmanaged, so that we can edit the defined values.

Use Cases

Now we know what environment variables are, but what would we actually use them for? Environment variables can come in handy in many different scenarios. They’re a great tool for implementing better ALM practices, where we can do things like update the link we reference for an environment everywhere we use that link by instead using an environment variable with a value we’d change per environment.

Another use case where we might be working on a SharePoint project, could be to use environment variables to set the site address and list names in our Power Automate flows and data sources in our Power Apps. Then when we import our solution to our target environment i.e. production, in order to change our site and lists (which ought to have the exact same properties i.e. columns!), we can simply change the value defined within our environment variable, and our solution will look at our defined site, and lists, that are created for production data!

Another example of when we might use an environment variable would be for things like a support email address. If you implement error handling in your Power Automate flows (hint hint again… you should!), then you might be sending error emails to a developer or support team if a flow runs into an issue. In this example, we could use an environment variable in the ‘to’ field of these send an email actions, and then the value we assign to that variable would be our support email address. But… the benefit… this means that we can really easily change our support email address across all of our flows if the support team or person changes! 10x easier and faster than going into EVERY flow to change the person in the ‘to’ field, right?!

Creating our environment variables

Creating our environment variables is easy! We simply need to go to a solution, click new, then click more, click environment variable, and then we can fill out the sidebar form to create an environment variable with its appropriate data type.

Creating an environment variable

Then if we open our environment variable to edit it, we can set a default value that will be used across the environments we deploy the variable to, and then we can further set a current value to override the default value to allow for differentiation of the value assigned depending on the environment we have deployed the variable to.

Referencing environment variables as data sources in Canvas Apps

So now we have these really cool environment variables, we need to look at how and where we can reference them for use. One of the places we can do this is in our data sources in a Canvas app. I’m going to take a SharePoint site and list as an example.

I have created two environment variables, one with a site address assigned as the value, and one with a list assigned using the data source data type.

Now in my Power Apps studio, I will go to data sources, add a new data source and search for SharePoint. Then instead of selecting one of the sites I have access to, I will click the advanced tab and select my environment variable for my site. Then again, when I need to choose a list, I will select advanced, and choose the environment variable for my list.

Now we’ve got an app with a data source that we can change from an environment variable level when we push our app into test or production.

Adding a data source in Canvas Apps using an environment variable

Referencing environment variables in a Power Automate flow

Next we’re going to look at how we can reference environment variables in a Power Automate flow as dynamic content. In fact, this is probably the easiest way we can reference environment variables in a solution.

So, first you need to create your flow or find one you want to start referencing an environment variable in. Then find the input you want to edit, and simply select your environment variable from the list of dynamic content.

Referencing environment variables in a flow

Referencing environment variables in Power Fx formulas

So, this is one a little less out-the-box, but it’s incredibly useful. It requires a little hack but it’s pretty easy to achieve! Power Apps doesn’t retrieve environment variables out the box for use in Power Fx formulas in the same way it might if we had defined a global or local variable in our application. So we need to do a little extra work to get the value of our variable.

To reference an environment variable in a Power Fx formula, we’re simply going to create a Power Apps triggered flow, we will use a compose action where we will make the inputs our environment variable, and then we will pass outputs to our Canvas app using the outputs of our compose action. Check out the flow below!

Flow to return an environment variable to Power Apps on a trigger within our application

So, now we need to run the flow we’ve created from within our app and set a variable with the outputs from the flow. This is really easy to do. Simply find a trigger for your flow, for example a button, and in the OnSelect property you want to use the following syntax.

Set(gblSiteUrl, FLOWNAME.Run().variable)

Setting a global variable with our flow outputs

Looking at our global variable assigned value after pressing our Set Variable button. The label has the variable in its ‘text’ property

So, as you can see, that’s a SUPER easy way to reference environment variables in your Power Fx formulas!

I hope this post helped you to get started with environment variables! If you have any questions, please do drop them in the comments below!

Written by
Lewis Baybutt
Microsoft Business Applications MVP • Power Platform Consultant • Blogger • Community Contributor • #CommunityRocks • #SharingIsCaring
Comments
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to LewisDoesDev.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.