Environment Variables in Dataverse and How to use it in Power Automate Flow
- Environment Variables
- Features of Environment Variables
- Create a new Environment Variable
- Use Environment Variable in Power Automate Flow
- ALM of Environment Variables
- Limitations
- Make use of the most in Environment Variables
Environment variables
Environment variables in Dataverse allows you to store the connection reference or keys which are need to be used inside the environment. They are more like a global variable which can be accessed with in the Power Platform Environment. We can make use of this to store the values and use it for Power Platform Customizations. We can use the environment variables in Canvas Apps, Model Driven Apps, and Power Automate. It allow you to modify the values while moving the solution to the another environment. These environment variables are really useful for avoiding the hardcode values for configuration data which needs to be changed during the ALM operations.
In Dataverse perspective the Environment Variable has two tables, Environment Variable Definition and Environment Variable Values. Environment Variable Definition table holds the definition, data type, name and some times default values(if it has). Environment variable value table has the current values that are used inside the current environment. Environment Variable Definition has 1:N relationship with the Environment Variable Value table.
Features of Environment Variables
- One Environment Variable can be used across multiple solution components.
- Six data types are supported by Environment Variable which are Decimal Number, Text, JSON, Yes/No, Secret and Data source
- There are two types of values which are
- Default Value
- Current Value
- It is part of the environment variable definition table. It is not a required if it has current values.
- If the value should be the same for all the environments, we may use default values. Because we can’t able to modify it in the target environment.
- It is part of environment variable value table. It will override the default value. It is used as a value even the default value contains a value.
- Remove the current value from the solution, if you don’t want to use the value in the target environment.
Create a new Environment Variable
- Go to the solution, where you want to create a new environment variable.
- Click on + New → More → Environment Variable. A quick create form pops from the right.
- Add Display Name, Name, Data Type and Value
- Click on save and publish
- I have created four environment variables which are tenant id, client id, client secret and scope. All are text data types.
- I have get the values from my Azure AD App and stored it in the Environment Variables for using it inside Power Platform.
- Note: You may use secret data type for client secret, for that you have to configure Azure Key Vault. As it is a learning purpose i have used text data type
Use Environment Variable in Power Automate Flow
- Create a new Power Automate Instant flow in a solution by navigating to +New → Automation → Cloud flow → Automated.
- Select Manually trigger a flow as a trigger and give a meaningful name to the flow.
- Click on Create and it will move the flow editor
- Add a step below the trigger and search for http and select the HTTP(premium) Connector.
- In the HTTP Action, Set the http method as POST and enter the uri as https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token, replace {tenant_id} in the uri with the environment variable(Tenant ID).
- In Headers, set Content-Type as key and application/x-www-form-urlencoded as value
- In Body, enter grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&scope={scope}, replace client_id, client_secret and scope with the environment variables.
- The HTTP step should appear like the below screenshot
- Save and Run the flow.
- It ran successfully with no errors
ALM of Environment Variables
The environment variables from the solution can be exported and imported across environments. It is still editable if we export and import as unmanaged solution. But in real time scenarios unmanaged solutions are not much preferred to get imported to the environment in terms of ALM.
To tackle this, we have to remove(not delete) the current value for all the environment variables and export the solution it as managed. While importing the solution to the target environment, a prompt open up and asking for the values for that environment variables.
Enter the values to the fields in the prompt and click on import to import the solution. After importing, the flows will work on the new values entered in the prompt.
Limitations
If the flows are using the environment variable values, but the value gets changed from the solution. But the flow still uses the previous value. The flow need to be turned on and off to catch up the modified value.
Make use of the most in Environment Variables
- Prefer to use current values than default values. As of now we can modify the values of the environment variables from the table or in Advanced find, even if it is a managed solution.
- We can use the environment variables in Model Driven App using JavaScript or Plugin.
- Better avoid hardcoding of some configuration values and connection keys in the code or in flows. Rather use Environment variables for those values.