Skip to main content

Notifications

Dynamics 365 Community / Forums / Sales forum / Environment variable
Sales forum
Unanswered

Environment variable

Posted on by 7
We have a specific value for each environment that should be set on account and lead form.
Does anyone have an example Javascript to achieve this?
Categories:
  • Luis Arellano Profile Picture
    Luis Arellano 34 on at
    Environment variable
     
    If you want to use the function it is a little more complicated:
    https://{yourOrg}.crm.dynamics.com/api/data/v9.2/RetrieveEnvironmentVariableValue(DefinitionSchemaName='{envVariableSchemaName}')
     
    Using Xrm.WebApi you have to include the metadata, following Microsoft's examples:
    var Sdk = window.Sdk || {};
    
    Sdk.RetrieveEnvironmentVariableValueRequest = function(DefinitionSchemaName) {
        this.DefinitionSchemaName = DefinitionSchemaName;
    };
    
    // NOTE: The getMetadata property should be attached to the function prototype instead of the
    // function object itself.
    Sdk.RetrieveEnvironmentVariableValueRequest.prototype.getMetadata = function () {
        return {
            boundParameter: null, // not boud function
            operationName: "RetrieveEnvironmentVariableValue",
            operationType: 1, // because it is a function
            parameterTypes: {
                "DefinitionSchemaName": { // Parameter name
                    "typeName": "Edm.String", // Copy the type from the parameter in the function's Microsoft learn page
                    "structuralProperty": 1 // Primitive type, we are looking for a string which is the environment variable name
                }
            },
        };
    };
    
    var retrieveEnvironmentVariableValueRequest = new Sdk.RetrieveEnvironmentVariableValueRequest("{envVariableSchemaName}"); 
    
    Xrm.WebApi.online.execute(retrieveEnvironmentVariableValueRequest).then(function (response) {
        if (response.ok) {
            return response.json(); // handle the promise result, you can either return or use the .then method
        }
    })
    .catch(function(error) {
          debugger; console.log(error.message);
        // handle error conditions
    });
     
    Your promise result should look like this
     
     
     
     
     
    If you are OK querying the table directly, you can take a look at these 2 posts.
     
    It is a regular XrmWebApi/rest call to the table
    environmentvariabledefinition
     
    I am copying one of the snippets shown in the first link
     
    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;
    }
     
     

Helpful resources

Quick Links

Dynamics 365 Community Update

Welcome to the inaugural Community Platform Update. As part of our commitment to…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Community Spotlight of the Month

Kudos to Mohana Yadav!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,142 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,979 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans