Get environment variables value using JavaScript

As application grow, the complexity of managing and maintaining their Dynamics 365 environments increases. With different teams working on different aspects of the system, it becomes challenging to ensure that the right values are used in the right places. One way to address this challenge is to use environment variables.

Environment variables are a feature in Dynamics 365 that allow you to define values once and use them in multiple places throughout the system. This can help you maintain consistency, reduce the risk of errors, and make it easier to update values in the future.

Environment variables has two values, one is the default value and another one is current value. The default value is constant among all the environment which can be moved from one environment to another, whereas the current value is pertained to the environment and can be added at any time.

Now we have the entity to store the config, how to get the stored config value in JavaScript. There are many ways you are able to retrieve the config from Environment Variables. There is a inbuilt function to get this information without using multiple retrievemultiple calls. Using below code will do a SDK request and use the “RetrieveEnvironmentVariableValue” method to get the value that is needed.

var Sdk = window.Sdk || {};
    Sdk.RetrieveEnvironmentVariableValueRequest = function (definitionSchemaName) {
        this.DefinitionSchemaName = definitionSchemaName;
    };
    Sdk.RetrieveEnvironmentVariableValueRequest.prototype.getMetadata = function () {
        return {
            boundParameter: null,
            parameterTypes: {
                "DefinitionSchemaName": {
                    "typeName": "Edm.String",
                    "structuralProperty": 1
                }
            },
         operationType: 1,
         operationName: "RetrieveEnvironmentVariableValue"
    };
};

var retrieveEnvironmentVariableValueRequest = new Sdk.RetrieveEnvironmentVariableValueRequest({EnvironmentVariableSchemaName});

Xrm.WebApi.online.execute(retrieveEnvironmentVariableValueRequest).then(
function success(result) {
    if (result.ok) {
        result.json().then(
            function (responseBody) {
                console.log(responseBody.Value);
                        
            });
    }
},
function (error) {
    Xrm.Utility.alertDialog(error.message);
});

Advertisement

2 thoughts on “Get environment variables value using JavaScript

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 )

Facebook photo

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

Connecting to %s