Set Environment Variables with PowerAutomate

Let’s say you have multiple envrionments, a solution with a lot of envrionment variables. Some of them are ID of an element (SharePoint list ID, Office 365 group ID…) and it can be very complicated to set everything on which environment. Even worse, as I am writing this article (11th October 2021), if you deploy a solution with values into your envrionment variables, it can erase and replace the actual value into your environment.
That’s why, now in our solution, we integrate a flow, which will set all the value of my environment variables.
Please note : To do so, we are going to use Dataverse. A prenium license is mandatory.

  1. Where and how are saved my Environment Variables?
  2. Set Environment variables values
    1. Information about the solution
    2. Into PowerAutomate flow
    3. Display bug while creating Env Var Value
  3. How to use it into a solution?

1. Where and how are saved my Environment Variables?

Your envrionment variables can be found into two Dataverse Tables :

  • Environment Variables Definition : Contains your environment variables, but not its value.
  • Environment Variables Value : Contains the current value of your envrionment variable, with a lookup to its definition.

2. Set Environment variables values

To set a Environment variable, be sure your variable (variable definition) exists into your environment.

2.1. Information about the solution

The process idea is as following :

  • Check if the environment variable value exist for your definition
  • If it exist :
    • Update the value
  • If it doesn’t exist :
    • Get the environment definition (to get its id)
    • Add a new line into the Environment variables value table

So let’s say I have a solution as following:

NameDescription
Microsoft DataverseConnexion reference to Dataverse.
MyEnvVarMy environment variable.
Set My Environment VariablesPowerAutomate flow to set my variable.

Please note that my environment variable have for display name MyEnvVar, but the Name (called SchemaName) is demo_MyEnvVar. This is really important, because the shcema name will never change, and we are going to use it to get my variable definition.
To find out my Schema Name, in your dashboard, you are going to have it here:
![](/content/images/2021/11/EnvironmentVar_FlowSchemaName.PNG)

2.2. Into PowerAutomate flow

So here is my test flow,
![](/content/images/2021/10/EnvironmentVarTestFlow_WholeFlow.PNG)


See picture in full screen
Firstable, I am going to initiate two flow variables, my environment variable schema name and the value I want to set.
![](/content/images/2021/10/EnvironmentVarTestFlow_initiateFlowVar.PNG)

Then, we are going to check if there is a Environment Variable value for our definition.

Need a copye/paste?

DefinitionValue
Filter rowsEnvironmentVariableDefinitionId/schemaname eq ‘[MY SCHEMA NAME]’
Expand QueryEnvironmentVariableDefinitionId

After that, we need to check if a value is returned or not.
To do so, we are going to check if the length of value returned is greater than 0. If so, it means a value exist.

Expression in conditionValue
Fxlength(outputs(‘Get_the_environment_variables_value’)?[‘body/value’])

If yes (or last condition returned 1 or more value. Normally just one).
We are going to update the Environment Variable Value.

We are use the Update a row action card.

NameValue
Table nameEnvironment Variable Values
Row IDitems(‘Apply_to_each_value_found_(normally_1)’)?[‘environmentvariablevalueid’]

If no, it means no environment variable value is found.
We are going to create the value into the dataverse Envrionment Variable Values table.
For that we need to get our Environment variable definition
We are using List rows action card.

NameValue
Table nameEnvironment Variable Definitions
Filter rowsschemaname eq ‘@{variables(‘My Var SchemaName’)}’

Then for the definition returned, which is only one if it exists, we are going to create an associate Environment Vaiable Value into the dataverse table.
We are using the Add a new row action card.

NameValue
Table nameEnvironment Variable Values
Schema Namevariables(‘My Var SchemaName’)
Valuevariables(‘New Value’)
2.3. Display bug while creating Env Var Value

In the case of your Variable definition exists, but not the value. You are going to run the flow, it will create your value, but while going into the Environment variable, you won’t see your value.


Do not worry, if you do a List Rows with PowerAutomate, you are going to find the value.
Also if clicking on Add existing, you are going to find back the value.

Another bug is, if you create the value by flow, sometime you have issue to edit it.


If it happens, delete the current value and recreate manually.

3. How to use it into a solution?

In this part, I am going to explain you how I use this flow in a project solution.

I am creating two flows:

  • One to set my envrionment values (such as described above), as a child flow
  • One which will manage the values

Firstable, my child flow, which will work as a function if you are/were a developper. It will, for a schema and a value set my variable.

See picture in full screen

Then my flow to manage my variables.
At the start, we are going to ask, while triggered, the environment.

Then, for all the variables which need to be set manually, we have a switch as below :
![](/content/images/2021/11/EnvironmentVar_ManageVarSwitch.PNG)

See picture in full screen

And for some variables, which can be retreive automatically, example here a Team from admin.powerplatform in my Environment (stored in Dataverse).
Our team has for name TeamExample. This Team is going to exist in all environments, but its ID will change from an environment to another.

And we are going to save its teamid into an environment variable.
Please note, “Equipes” means Teams in French.