How can I get the value of an Environment Variable using JavaScript?
"Note: the GUID of the environment variable value will be the same in the different environments if you imported the variable with a solution."
I know this is an old comment, but at this moment this statment is wrong. The GUID will differ based on the environments, even if you import it as part of a solution. Hopes this could save others some time.
Here is another example, This code expects an environment variable type JSON, example { "Url":"<URL>", "Login":"<LOGIN>", "PW":"<Password>" }
// Schemaname of the environment variable
var connectionDataEnVariable = "<YourEnviromentVariableSchemaName>";
// var results = await Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", "?$filter=schemaname eq " + connectionDataEnVariable + "&$select=environmentvariabledefinitionid&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)");
Xrm.WebApi.retrieveMultipleRecords("environmentvariablevalue", "?$select=value&$expand=EnvironmentVariableDefinitionId($select=schemaname)&$filter=EnvironmentVariableDefinitionId/schemaname eq '" + connectionDataEnVariable + "'").then(
function success(results) {
debugger;
if (results && results.entities && results.entities.length >= 1) {
var connectionData = results.entities[0].value;
}
else {
// Die Umgebungsvariable existiert nicht
alert("Umgebungsvariable " + connectionDataEnVariable + " wurde nicht gefunden.");
return;
}
if (connectionData) {
// Die Umgebungsvariable existiert
var data = JSON.parse(connectionData);
var url = data.Url;
var login = data.Login;
var pw = data.PW;
console.log("URL: " + url);
console.log("Login: " + login);
console.log("Passwort: " + pw);
}
},
function (error) {
debugger;
console.log(error.message);
}
);
Thanks @Kuyler! Yup, there’s now an out of the box function for this: https://learn.microsoft.com/power-apps/developer/data-platform/webapi/reference/retrieveenvironmentvariablevalue
cheers
To get the correct value use the "RetrieveEnvironmentVariableValueRequest" request with execute.
Example can found here: https://mytrial365.com/2021/03/15/get-environment-variables-value-using-javascript/
Hi @alanodev, I know this dates a while back but just thought I'll update this thread with the latest code sample to retrieved by environmental variable name:
async GetEnvironmentVariableValue function (name) {
let results = await Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", `?$filter=schemaname eq '${name}'&$select=environmentvariabledefinitionid&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)`);
if (!results || !results.entities || results.entities.length < 1) return null;
let variable = results.entities[0];
if (!variable.environmentvariabledefinition_environmentvariablevalue || variable.environmentvariabledefinition_environmentvariablevalue.length < 1) return null;
return variable.environmentvariabledefinition_environmentvariablevalue[0].value;
}
Note: users will need read privileges to Environment Variable Definition.
Cheers
Hi @alanodev,
You can do this with the out-of-the-box client web api script, retrieveRecord.
Xrm.WebApi.retrieveRecord("environmentvariablevalue", <GUID>, "?$select=value").then(successCallback, errorCallback);
Note: the GUID of the environment variable value will be the same in the different environments if you imported the variable with a solution. If you want to fetch by the name and you need to query with EnvironmentVariableDefinition entity.
More info on retrieve: https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrieverecord
Hope this helps!
What kind of environment variable and also where is your javascript sitting, is it a js running in a power apps model driven entity form, canvas app, or something else?