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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

use async function in sync function in javascript

(0) ShareShare
ReportReport
Posted on by 60

I have a simple asynchronous function to get the current enviorment variable .I am trying to use this function marked in red in another sync function but getting value as promise pending. I know I am making an error calling this function. Can I get the idea  how to call it and utilize it?

// Asysc to get enviornment variable

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;// returning the value that I want. current value of env varibale
}


//Calling it to another function and getting value as promise pending

UseCurrentValue : function (e) {
        //var formContext= e.getFormContext();  
      
        var alertStrings = { confirmButtonLabel: "Ok", text: "Please wait for it to open an new form.", title: "INFORMATION:" };
        var alertOptions = { height: 300, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
            function success(result) {

                var envURL=  getEnvironmentVariable("new_variable");// Promise Pending
                console.log(envURL);   // promise pending
  ...rest od the code..no need to see..
}}
I have the same question (0)
  • Verified answer
    Ray Profile Picture
    1,537 on at

    Actually, your code has used the Promise in this line: Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then

    As the openAlertDialog function also returns a Promise object.

    So just keep going like this:

    getEnvironmentVariable("new_variable").then(
        function (envURL) {
            // do your logic here
        }, function (error) {
            // deal with error
        }
    );

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans