Today we have a look at solution component Environment Variable Definition and Environment Variable Values in Dynamics 365. We took this blog from November 2019 that introduced us to this new solution components and put it to the test in regards of deployment.
Environment Variable Definitions and Values are used to handle environment specific configurations - for example webhooks that differ from dev, test or production.
Environment Variable Definitions can be created inside the Power App studio of your environment. They can be part of a solution and therefore fall into the category of a solution component.
An Environment Variable Definition can have a Name, Default Value, Data Type and a Current Value. There should be only one current value - we call this Environment Variable Value. Environment Variable Definition and Environment Variable Value share a 1:1 relationship.
Environment Variable Value can be updated, deleted or excluded from the solution:
When adding an existing Environment Variable Definition you can choose if you want to add the Environment Variable Value too:
Environment Variable Definition and Environment Variable Value currently can only be used by standard CDS API calls. This makes it more difficult to know how to handle the definitions and values.
Deployment of Environment Variable Definitions
For our scenario we will work with two environments: DEV and TEST. The solution will be exported as managed from DEV and imported in TEST.
Solution A: no default value and no Environment Variable Value
When importing a Solution A you will get asked to populate the current value of the Environment Variable Value as there is no default.
The default value will still be empty but a Environment Variable Value record will be created. We can see this using the Advanced Find:
This Environment Variable Definition can be edited or deleted but there can always be only one value per definition.
Solution B: default value without Environment Variable Values
When importing Solution B you will have no dialog to set the current value of the Environment Variable Definition. There will be no Environment Variable Values. As there are no specific action related to Environment Variable in the CDS we can only query the records for the entity Environment Variable Definition or Environment Variable Value.
Solution C: no default value with Environment Variable Values
When importing Solution C you will see a dialog asking for the current value of the Environment Variable Definition. Because we included the DEV Environment Variable Value it auto-suggests the value:
We realized that on importing the Solution C the GUID of the Environment Variable Value stays the same. The GUID can be found as usual in the URL of the Environment Variable Value record or in the JSON created in the solution zip:
Even though the solution is managed you are allowed to overwrite the values. If you import the solution again the target Environment Variable Value will NOT be updated.
This means that in CDS queries you could create the fixed GUID so you can save yourself a multiple retrieve.
Additionally the record created during the import of the managed solution cannot be deleted on the target environment as it counts as a managed component.
In general as there are only the common CDS actions we do not recommend mixing default value and Environment Variable Value. Instead you should create your logic to use Environment Variable Values with fixed GUID that is only provided in Solution C. This is very important as the Environment Variable Values have no alternate key. Additionally, the having the Environment Variable Value initially created via solution import as in Solution C you are not able to delete or replace the record. That way you make sure your logic prevails.
In your queries you can use the deployed GUID like this: