Sync Calls with Async-Await

While working with JavaScript, often we use XRM.WebAPI to query Customer data. Sometimes requirements make us retrieve data synchronously. Till now we were using XMLHTTPRequest with async property set to true or false to specify WebAPI call to be async or sync. However, this method is not available with v9.0 Xrm.WebApi. for sequential calls, you have to do nested success callbacks. This will return a Promise object and executed in browser.

Alternatively, now we can use “aysnc” and “await” keywords in your JavaScript and achieve similar results.

For example, we want to check if an opportunity exists on change of child record and perform some business logic, we need to make a sync call.

To achieve this, we need to decorate method with “async” keyword and use “await” before invoking my method (getopportunity).

getopportunity: async function (opportunityId) {
        var data = null;
        await"opportunity", opportunityId, "?$select=new_eventendtime,new_eventstarttime").then(
            function success(result) {
                data = result;
            function (error) {
        return data;

ChildRecordChanged: async function (executionContext) {
        var formContext = executionContext.getFormContext();
        var opportunityobject = formContext.getAttribute('opportunityid').getValue();
	 var result = await getopportunity (opportunityobject [0].id.replace('{', '').replace('}', ''));

            if (result != null) {
                //business logic goes here

The word “async” ensures that the function returns a promise, and wraps non-promises in it.

The keyword “await” makes JavaScript wait until that promise settles and returns its result.

Await only works within Async functions