Back to Basics # 62: Retrieve Environment Variable Value with Webresource in Dynamics CRM

Introduction:

Applications often require different configuration settings or input parameters when deployed to different environments. Environment variables store the parameter keys and values, which then serve as input to various other application objects. Separating the parameters from the consuming objects allows you to change the values within the same environment or when you migrate solutions to other environments. The alternative is leaving hard-coded parameter values within the components that use them. This is often problematic; especially when the values need to be changed during application lifecycle management (ALM) operations. Because environment variables are solution components, we can transport the references (keys) and change the values when solutions are migrated to other environments. As an example, on selected contact record an -environment variable value will be retrieved based on the schema name using webresource.

Step 1:

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the   below figure.

Step 2:

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Step 3:

After Step 2, use below code to retrieve value from environmentvaruablevalue entity and use expand query to query EnvironmentVariableDefinition table by passing required schema name to retrieve

let result= await Xrm.WebApi.retrieveMultipleRecords(“environmentvariablevalue”,”?$select=value&$expand=EnvironmentVariableDefinitionId&$filter=(EnvironmentVariableDefinitionId/schemaname eq ‘”+websiteurlEnvschemaname+”‘)”);

As shown in the below figure

Step 4:

After Step 3,  include the above logic inside a function getEnvironmentVariable with the below code

async function  getEnvironmentVariable(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

        let websiteurlEnvschemaname=”cr5bc_PersonalWebSiteUrl”;

        try {

           let result= await Xrm.WebApi.retrieveMultipleRecords(“environmentvariablevalue”,

           “?$select=value&$expand=EnvironmentVariableDefinitionId&$filter=(EnvironmentVariableDefinitionId/schemaname eq ‘”+websiteurlEnvschemaname+”‘)”);

           console.log(“Retrieved environment variable Url: ” + result.entities[0].value );

        }

        catch (error) {

            Xrm.Navigation.openErrorDialog({ details: error.message, message: ‘A problem occurred while retrieving an Environment Variable value. Please contact support.’});

        }

    }

}

And as shown in the below figure.

Step 5:

After Step 4, call getEnvironmentVariable function from handleOnLoad function , and the final code looks like below

if (typeof (ContosoVaccination) == “undefined”)

{

    var ContosoVaccination = {__namespace: true};

}

if (typeof (ContosoVaccination.Scripts) == “undefined”)

{

    ContosoVaccination.Scripts = {__namespace: true};

}

ContosoVaccination.Scripts.ContactForm =

{

    handleOnLoad: function (executionContext)

        {

        console.log(‘on load – contact form’);

        getEnvironmentVariable(executionContext);

        },

    __namespace: true

}

async function  getEnvironmentVariable(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

        let websiteurlEnvschemaname=”cr5bc_PersonalWebSiteUrl”;

        try {

           let result= await Xrm.WebApi.retrieveMultipleRecords(“environmentvariablevalue”,

           “?$select=value&$expand=EnvironmentVariableDefinitionId&$filter=(EnvironmentVariableDefinitionId/schemaname eq ‘”+websiteurlEnvschemaname+”‘)”);

           console.log(“Retrieved environment variable Url: ” + result.entities[0].value );

        }

        catch (error) {

            Xrm.Navigation.openErrorDialog({ details: error.message, message: ‘A problem occurred while retrieving an Environment Variable value. Please contact Admin.’});

        }

    }

}

Step 6:

After Step 5, save script and register it on the contact form on load event and publish it , and open any contact record and observe console window for the resultant environment variable value as shown in the below figure

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. In code for readability and to take screenshots retrievemultiple code was shown in 2 lines , but it should be written in a single line otherwise we get errors during form load.
  3. Microsoft documentation for environment variables can be found here.
  4. There are otherways of retrieval through environmentvariabledefinition table as well.
  5. Make sure to provide Read access privileges for these environment related tables for the user role , otherwise runs into permission issues.
  6. It is always recommended to add any environment variable in a solution as shown in the below figure

Conclusion: In this way, one can easily get Environment variable value using Webresource(javascript).

One thought on “Back to Basics # 62: Retrieve Environment Variable Value with Webresource in Dynamics CRM

  1. Pingback: Back To Basics : Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

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