Retrieving environment variable value in Javascript

By | November 12, 2020

The script below might help if you wanted to read CDS environment variable value in your javascript web resource.

top.environmentVariables = [];

function getEnvironmentVariableInternal(varName){
  "use strict";
   top.environmentVariables[varName] = null;
   Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", `?$top=1&fetchXml=<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
	  <entity name='environmentvariabledefinition'>
		<attribute name='defaultvalue' />
		<filter type='and'>
		  <condition attribute='schemaname' operator='eq' value='` + varName + `' />
		</filter>
		<link-entity name='environmentvariablevalue' from='environmentvariabledefinitionid' to='environmentvariabledefinitionid' link-type='outer' alias='varvalue'>
		<attribute name='value' />      
		</link-entity>
	  </entity>
	</fetch>`).then(
		function success(result) {
			for (var i = 0; i < result.entities.length; i++) {
			        if(typeof(result.entities[i]["varvalue.value"]) != "undefined")
                                {
                                   top.environmentVariables[varName] = result.entities[i]["varvalue.value"];
                                }
				else if(typeof(result.entities[i].defaultvalue) != "undefined")
                                {
                                   top.environmentVariables[varName] = result.entities[i].defaultvalue;
                                }
                                else{
                                   top.environmentVariables[varName] = null;
                                }
			}                    
		},
		function (error) {
			console.log(error.message);
			
		}
	  );
	  
}

function getEnvironmentVariable(executionContext)
{
  "use strict";
   getEnvironmentVariableInternal("SCHEMA_NAME_OF_YOUR_VARIABLE");	
}

Just a couple of notes:

1. I’m using WebAPI + FetchXML to get the values

I think this is just because I’m so used to FetchXml it’s my first choice. As Diana Birkelbach just noted, it should actually be easier with Web API. So will be updating this post soon to add a Web API – only version of the same function.

2.  I’m storing variable value (default or overridden) in the top.environmentVariables array

This way, I can access that array from the script associated to a ribbon button (which is a completely separate script)

PS. As promised, here is an updated version that’s not using FetchXML:

top.environmentVariables = [];

function getEnvironmentVariableInternal(varName){
  "use strict";
   top.environmentVariables[varName] = null;
   Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", "?$select=defaultvalue,displayname&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)&$filter=schemaname eq '"+varName+"'").then(
		function success(result) {
			for (var i = 0; i < result.entities.length; i++) {
			        if(typeof(result.entities[i]["environmentvariabledefinition_environmentvariablevalue"]) != "undefined"
                                   && result.entities[i]["environmentvariabledefinition_environmentvariablevalue"].length > 0)
                                {
                                   top.environmentVariables[varName] = result.entities[i]["environmentvariabledefinition_environmentvariablevalue"][0].value;
                                }
                                else if(typeof(result.entities[i].defaultvalue) != "undefined")
                                {
                                   top.environmentVariables[varName] = result.entities[i].defaultvalue;
                                }
                                else{
                                   top.environmentVariables[varName] = null;
                                }
			}                    
		},
		function (error) {
			console.log(error.message);
			
		}
	  ); 
}


function getEnvironmentVariable(executionContext)
{
  "use strict";
   getEnvironmentVariableInternal("coo_InvoicePrintFlowUrl");	
}

 

2 thoughts on “Retrieving environment variable value in Javascript

  1. Niels Steen

    This solutions works looks very clean, but the end users would still need read rights on environment variables correct? Is there a clean way to impersonate an admin user in the request, circumventing that?

    Reply

Leave a Reply

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