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 :
Microsoft Dynamics CRM (Archived)

By default all the WebAPI calls are Asynchronous in Dynamics 365 V9, how do we make them Synchronous?

(0) ShareShare
ReportReport
Posted on by

By default all the WebAPI calls are Asynchronous in Dynamics 365 V9, how do we make them Synchronous WebAPI calls? 

We are using Dynamics 365 online V9.x version (https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/xrm-webapi/retrieverecord )

Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback);

This is the new WebApi java script to retrieve record, in this syntax there is no option to make this call synchronous, by default its taking asynchronous.

*This post is locked for comments

I have the same question (0)
  • David Jennaway Profile Picture
    14,065 on at

    If you're calling from the client-side using XMLHttpRequest, then set the async parameter to false in the open method (the async parameter is the 3rd parameter):

    var req = new XMLHttpRequest();
       req.open("POST", encodeURI(getWebAPIPath() + entitySetName), false);

    Note that synchronous XMLHttpRequest calls are deprecated in most browsers (unless you run them on a worker thread), due to potential performance overhead.

  • Community Member Profile Picture
    on at

    Thanks for the reply, we are using Dynamics 365 online V9.x version (docs.microsoft.com/.../retrieverecord)

    Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback);

    This is the new WebApi java script to retrieve record, in this syntax there is no option to make this call synchronous, by default its taking asynchronous.

  • Verified answer
    Daniel Wikell Profile Picture
    2,360 on at

    The short answer is that you are not supposed to make them synchronous since synchronous calls locks the browser while the call is processing. With async being the only option, MS encourages you to design your code asynchronously as well.

    If you want some code to execute synchronously after your Query you will simply have to move that code into a successCallback function.

    If you have multiple queries that you want to execute in order you will quickly run into something called Callback Hell (http://callbackhell.com/) so be sure to design wisely.

    Here's another page describing how to design for asynchronous calls that also brings up the use of Promises. Since the Xrm.WebApi does return a Promise already you should be able to build upon that.

    medium.freecodecamp.org/javascript-from-callbacks-to-async-await-1cc090ddad99

  • Ndewitt Profile Picture
    47 on at

    can get the equivalent result of by using async/ await

    try {

        const result = await Xrm.WebApi.retrieveRecord(entityLogicalName, id, options);

        // do something with result now, it's resolved

    }

    catch(error) {

    console.log(error)

    }

    That doesn't actually block the main thread, it's just a much nicer syntactic way of resolving promises and handling errors.

  • Suggested answer
    Community Member Profile Picture
    on at

    Hello,

    Please use Promise and calling function has to be async. Sample code is provide below :   

    async function SampleRequest() {
    try{
    var contactId = "***************";
    var ContactEmailAddress = await RetreieveContactEmailID(contactId);

    }catch (error) {
    console.error("Error : " + error);
    }

    }

    // Function to Retreive Email Address from Contact Entity ......................................................................................
    function RetreieveContactEmailID(ContactGUID) {
    return new Promise(function (resolve, reject) {
    Xrm.WebApi.retrieveRecord("contact", ContactGUID, "?$select=firstname,emailaddress1").then(
    function success(result) {
    if (result.emailaddress1) {
    resolve(result.emailaddress1);
    }
    else {
    resolve(null);
    }
    },
    function (error) {
    reject(error.message);
    }
    );

    });
    }

    Thanks,
    Sumit

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans