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+"')");
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.'
});
}
}
}
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
- Make sure to publish all customizations and upload JavaScript (js) file.
- In code for readability and to take screenshots retrieve multiple code was shown in 2 lines, but it should be written in a single line otherwise we get errors during loading of form.
- Microsoft documentation for environment variables can be found here
- 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).