CDS: Environment Variables

Have you ever wondered where you can keep your configuration settings while using Dynamics 365 or CDS? Old way we used to do it was to use a new custom entity to store such Global settings but I have discovered this feature which has been introduced to solutions which is called Environment Variables.

You can create environment variables though solutions or using Code.

So to create an environment variable , go to make.powerapps.com and create a new solutions and click new and Select new environment URL:

The setup your environment variable by adding its name and Data Type, Available Data Types:

Decimal, JSON, Text, Two Options

How to access the Environment Variables?

Using JS through your Model Driven App as below:

First you need to Query the EnvironmentVariableDefinition to get the environmentvariabledefinitionid:

Xrm.WebApi.online.retrieveMultipleRecords("environmentvariabledefinition", "?$select=displayname,environmentvariabledefinitionid,schemaname&$filter=schemaname eq 'cr2ad_ServiceURL'").then(
    function success(results) {
        for (var i = 0; i < results.entities.length; i++) {
            var displayname = results.entities[i]["displayname"];
            var environmentvariabledefinitionid = results.entities[i]["environmentvariabledefinitionid"];
            var schemaname = results.entities[i]["schemaname"];
        }
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);

Then use the environmentvariabledefinitionid to get the Environment Variable value as below:

Xrm.WebApi.online.retrieveMultipleRecords("environmentvariablevalue", "?$select=value&$filter=_environmentvariabledefinitionid_value eq 7d1d08cc-c9d7-ea11-a813-000d3a6aa9c8").then(
    function success(results) {
        for (var i = 0; i < results.entities.length; i++) {
            var value = results.entities[i]["value"];
        }
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);

Then the Value will be the environment Variable you have saved!

Using Plugin:

You can query the EnvironmentVariableDefinition and EnvironmentVariableValues in your query expressions the same same way with other entities/

Using Flow to get Environment Variable:

where Item ID:
first(outputs('List_records')?['body/value'])['environmentvariabledefinitionid']

Hope it is useful!

2 thoughts on “CDS: Environment Variables

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: