web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

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

(0) ShareShare
ReportReport
Posted on by

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.

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    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.

  • Verified answer
    Community Member Profile Picture
    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!:)

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

    Yes it got resolved

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans