Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Environment variable

(1) ShareShare
ReportReport
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?
  • Luis Arellano Profile Picture
    Luis Arellano 36 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;
    }
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans