Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to pass environment variable using js code in dynamics 365 form

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

i have the below js code added to button, where onclick on that button http flow will trigger

 triggerFlow: function(formContext) {
            var CJGuid = formContext.data.entity.getId();
            CJGuid = CJGuid.replace(/[{}]/g, "");
            parent.$.ajax
                ({
                    type: "POST",
                    url: "https://.....165.westeurope.logic.azure.com:443/workflows/f2ab0bd0a17b4c26bfdec6a74cd04f08/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig='....",
                    contentType: 'application/json',
                    data: JSON.stringify({ "CJGuid": CJGuid }),
                    success: function () {
                        alert("Review has been successfully completed");

                    }
                });

but when we try to import the solution as managed to production the url will change.

so in the js code just want to pass the url as environment variable so that while importing to product environment we can change the url , because we cannot edit the js code in managed solution

Retrieving Environment Variable Values via JavaScript (Microsoft Dataverse / Power Apps) (crmchap.co.uk)

got this blog post, but not sure how to implement on my above code, any help would be appreciated thank you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to pass environment variable using js code in dynamics 365 form

    Yes it got resolved

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to pass environment variable using js code in dynamics 365 form

    Hi psk_12,

    So your problem has been solved?

    Glad it's solved and thanks for sharing!:)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to pass environment variable using js code in dynamics 365 form

    function callHTTPFlow(primaryControl) {

    "use strict";

    getEnvironmentVariable("azmdc_flowurl",

       function(url){

    callHTTPFlowInternal(url, primaryControl);

       },

    function(error){

    handleError(error);

    }

    );

    }

    function callHTTPFlowInternal(url, primaryControl) {

    "use strict";

       var CJGuid = primaryControl.data.entity.getId();

       CJGuid = CJGuid.replace(/[{}]/g, "");

       parent.$.ajax

           ({

               type: "POST",

               url: url,

               contentType: 'application/json',

               data: JSON.stringify({ "CJGuid": CJGuid }),

           });

    }

    function getEnvironmentVariable(varName, onSuccess, onError){

     "use strict";

      Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", "?$select=defaultvalue,displayname&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)&$filter=schemaname eq '"+varName+"'").then(

    function success(result) {

    var varValue = null;

    for (var i = 0; i < result.entities.length; i++) {

    if(typeof(result.entities[i]["environmentvariabledefinition_environmentvariablevalue"]) !== "undefined"

      && result.entities[i]["environmentvariabledefinition_environmentvariablevalue"].length > 0)

    {

      varValue = result.entities[i]["environmentvariabledefinition_environmentvariablevalue"][0].value;

    }

    else if(typeof(result.entities[i].defaultvalue) !== "undefined")

    {

      varValue = result.entities[i].defaultvalue;

    }

    else{

      varValue = null;

    }

    }    

    onSuccess(varValue);

    },

    function (error) {

    onError(error);

    }

    );

    }

    this is the code i used to pass environment variable and it worked perfectly.

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,684 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,414 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans