How to get value of Environment variable in Model driven apps/ Dynamics 365 javascript

Hello everyone and welcome to my blog. In today’s blog I will discuss how to get the value of Environment variables of DataVerse in Dynamics 365/ Model Driven apps.

I have the following environment variable in my environment and I want to retrieve the value of it in my form script code.

To understand how we can retrieve the value of environment variable, we need to understand the schema of how it is stored in DataVerse.

There are two underlying tables.

  • Environmentvariabledefinition
  • Environmentvariablevalue

The schema information is stored in the table Environmentvariabledefinition and the value of it in Environmentvariablevalue.

And below is the WebAPI query to retrieve the value.

https://<orgurl>/api/data/v9.2/environmentvariabledefinition?$filter=<schemaname>&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)

In my example, below is the URL to fetch.

https://<orgurl>/api/data/v9.2/environmentvariabledefinition?$filter=aib_dbconnection&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)

Since I have to retrieve this value in javascript, I will use Xrm.WebApi.retrieveMultiple. Below is the complete code,

// JavaScript source code
function retrieveEnvironmentVariable(e) {
   debugger;
   var schemaName = 'aib_dbconnection';
   Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", "?$filter=schemaname eq '"
      + schemaName + "'&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)")
      .then(function (result) {
         if (result.entities && result.entities.length > 0) {
            var record = result.entities[0];
            if (record.environmentvariabledefinition_environmentvariablevalue
               && record.environmentvariabledefinition_environmentvariablevalue.length > 0) {
               // get the value of environment variable
               var value = record.environmentvariabledefinition_environmentvariablevalue[0].value;
            }
         }
      }, function (error) {
            console.log(error.message);
      });
}

And there you have the environment variable.

Hope you liked this post. If this post has helped, you can buy me a coffee.

For similar topics on Microsoft.NET and Power Platform, subscribe to my blog using the Subscribe option on right pane.

Debajit Dutta
Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading