In Dynamics 365, Environment Variables let you externalize configurable values such as URLs, keys, or feature toggles without changing the solution code.
Each variable can have:
When working with JavaScript, especially on forms or PCF controls, you may need to retrieve the effective value of an environment variable—preferably the Current Value if available, falling back to Default Value.
✅ Recommended Approach: Use FetchXML with link-entity
Since the Web API OData endpoint for environmentvariabledefinition does not expose environmentvariablevalue as a navigation property, we use FetchXML to join the two tables in one call.
✅ Sample Function
/**
* Retrieves the effective value of an environment variable using FetchXML.
* Prioritizes the override "Current Value" from environmentvariablevalue, falls back to "Default Value".
*
* @param {string} varName - The schema name of the environment variable.
* @param {function(string):void} successCallback - Called with the resolved value.
* @param {function(string):void} errorCallback - Called with an error message if any error occurs.
*/
function comm_GetEnvironmentVariableValue(varName, successCallback, errorCallback)
{
var fetchXml =
"<fetch top='1'>" +
" <entity name='environmentvariabledefinition'>" +
" <attribute name='environmentvariabledefinitionid'/>" +
" <attribute name='defaultvalue'/>" +
" <filter>" +
" <condition attribute='schemaname' operator='eq' value='" + varName + "'/>" +
" </filter>" +
" <link-entity name='environmentvariablevalue' from='environmentvariabledefinitionid' to='environmentvariabledefinitionid' link-type='outer'>" +
" <attribute name='value'/>" +
" </link-entity>" +
" </entity>" +
"</fetch>";
var encodedUrl = "?fetchXml=" + encodeURIComponent(fetchXml);
Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", encodedUrl).then(
function(result)
{
if (result.entities.length > 0)
{
var record = result.entities[0];
var defaultVal = record["defaultvalue"];
// Find override value (alias from link-entity)
var overrideKey = Object.keys(record).find(k => k.endsWith(".value"));
var overrideVal = overrideKey ? record[overrideKey] : null;
var effectiveVal = overrideVal || defaultVal;
successCallback(effectiveVal);
}
else
{
errorCallback("Environment variable '" + varName + "' not found.");
}
},
function(error)
{
errorCallback("Error retrieving environment variable: " + error.message);
}
);
}
๐งช How to Debug from Browser Console
You can quickly test this function by running the following debug version in your browser console (inside a Dynamics 365 form):
function debug_GetEnvVarValue_FetchXML()
{
var varName = "hx_CreateDocumentLocationFlowUrl"; // Replace with your variable's schema name
comm_GetEnvironmentVariableValue(varName,
function(val)
{
console.log(varName + ":", val);
},
function(err)
{
console.error("Error:", err);
}
);
}
๐ Notes
-
This approach does not require two API calls, making it efficient and cleaner.
-
FetchXML link-entity uses link-type='outer' to gracefully handle cases where no override exists.
-
Works for all simple types (Text, Number, JSON, etc.).
๐ Summary
To reliably retrieve the current value of an environment variable in client-side code:
-
Use a fetchXml query that links environmentvariabledefinition and environmentvariablevalue.
-
Look for .value keys in the returned result.
-
Fall back to defaultvalue if no override is present.
This method is tested and safe for use in production Dynamics 365 environments.
great
ReplyDeleteThe distinction between a Default Value and a Current Value is particularly useful when working with Dynamics 365 Environment Variables, since the effective configuration can vary between environments without requiring changes to the solution code. The approach described here also addresses a practical limitation when retrieving these values through JavaScript.
ReplyDeleteUsing FetchXML with a link-entity provides a clean way to retrieve the related environment variable value in a single request. I like the priority given to the Current Value with a fallback to the Default Value, since that matches the need for environment-specific configuration while still providing a reliable value when no override exists. This kind of implementation is a useful application of Javascript Online Training concepts in Dynamics 365 customization.
The example is also a good reminder that knowing how the underlying tables relate can be important when an OData navigation property is not available in the expected form. Combining FetchXML and client-side JavaScript gives developers a practical way to work with configuration data while keeping environment-specific values outside the application logic. This makes Javascript Online Course relevant for developers building reusable Dynamics 365 form and PCF solutions.
ReplyDelete