A while back, I had seen that you could use JSON within your Environment Variables in D365/PowerApps, and this got my brain going as to how they could fit into client-side development when configured via the server (and when being deployed via Pipelines).
With that in mind, I created a few Environment Variables where I could test out the complexity of JSON along with how the system could use the Default/Current values when deploying code via Pipelines.

The goal was pretty simple: render some notification bars in different scenarios, showing what happens when you access the different types of JSON data.

In the first two cases (of simple and complex), I have provided current values that were deployed as part of the solution. In the third environment variable, we provide the default value, but not the current value, therefore providing a graceful backup option in case the value is not supplied.
To load the base Environment Variables, I wrote the following JavaScript function to load the defaults.
let FORM_CONTEXT = null;
function onLoadLeads(executionContext) {
FORM_CONTEXT = executionContext.getFormContext();
console.log(FORM_CONTEXT);
LoadDefaults("all_JsonSimple",ShowSimple);
LoadDefaults("all_JSonComplex",ShowComplex);
LoadDefaults("all_JsonDefault",ShowSimple);
}
The script below does the heavy work of getting the environment variables themselves. The trickiest part was getting the schema evaluation correct to not only retrieve the current value but the default value as well to then fall back on.
function LoadDefaults(schema, callback) {
const query = "?$select=environmentvariabledefinitionid,defaultvalue,displayname,schemaname&$expand=environmentvariabledefinition_environmentvariablevalue($select=environmentvariablevalueid,value)&$filter=schemaname eq '" + schema + "'"
Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", query).then(
function success(result) {
if (result.entities.length > 0) {
const enviro = result.entities[0];
console.log("Default Value: " + enviro.defaultvalue);
console.log("Display Name: " + enviro.displayname);
let data = null;
if (enviro.environmentvariabledefinition_environmentvariablevalue.length > 0) {
data = enviro.environmentvariabledefinition_environmentvariablevalue[0].value;
}
//We can retrieve our default data if no data was provided
if (!data && enviro.defaultvalue) {
data = enviro.defaultvalue;
}
callback(data,schema);
} else {
ShowError(schema);
}
},
function (error) {
ShowError(schema);
}
);
}
There are a few first use cases where I could see this being useful in a PowerApps/D365 environment when you want to apply consistent, structured base configurations across your system without creating ANOTHER configuration table to do so.