Skip to content

Retrieve EnvironmentVariableValue in JavaScript

Posted in Power Platform, and Dynamics 365

In my private project (Key Box), I stored a JSON configuration in an Environment Variable. To retrieve its value, I had could use two queries. The first query to retrieve the GUID of the Environment Variable Definition and the second query to retrieve the Environment Variable Value. But there is a better way: the RetrieveEnvironmentVariableValue WebApi function.

Below is the async function I wrapped around it to make it easy reusable.

Retrieve EnvironmentVariableValue JavaScript

async function getEnvironmentVariableValue (schemaName) {
  return Xrm.WebApi.online
    .execute({
      DefinitionSchemaName: schemaName,
      getMetadata: function () {
        return {
          boundParameter: null,
          parameterTypes: { 
            DefinitionSchemaName: { 
              typeName: "Edm.String", 
              structuralProperty: 1, 
            } ,
          },
          operationType: 1,
          operationName: "RetrieveEnvironmentVariableValue",
        };
      },
    })
    .then(function success(response) {
      if (response.ok) {
        return response.json();
      }
    })
    .then(function (responseBody) {
      return responseBody["Value"];
    })
    .catch(function (error) {
      console.error(error.message);
    });
}

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.