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)

CRM Web API Synchronous Vs Asynchronous

(0) ShareShare
ReportReport
Posted on by 397

Hi,

I was trying to explore the WebApi synchronous calls. I need to make multiple api calls in a method to get the counts. In below sample code, TestCall method i need to make 3 webapi calls but i am unable to retrieve the result of three methods because of Asynchronous call.

I have gone through the post https://community.dynamics.com/crm/f/117/t/281784 to use the callback function but unable to determine how to use my dynamic method to return the results of 3 webapi calls (I am able to get the results using call back but not sure how to use the same dynamically and use the results in my business logic). I have pasted the sample code that i was trying to learn. Technically i am trying to learn Synchronous calls using WebApi or how to implement these kind of things using WebApi.

function TestCall(selectedContact,testid)
{

var id = selectedContact.replace('{','').replace('}','');
var contactLeadCount = LeadApiCall("leads",id,"_parentcontactid_value");
var oppContCount = OpportunityApiCall("opportunities", id, "_parentcontactid_value");
var accountContCount = AccountApiCall("accounts",id,"_primarycontactid_value");

//Business logic based on the results of counts....from LeadApiCall, OpportunityApiCall, AccountApiCall


}

function EntityApiCall(entityname,entityid,entitykey)
{

var req = new XMLHttpRequest();
var results = null;
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/"+ entityname + "$filter=" + entitykey + "eq " + entityid , true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4)
{
req.onreadystatechange = null;
if (this.status === 200)
{
results = JSON.parse(this.response);
callBack(results.value.length);

} else
{
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}


function callBack(count)
{
alert("Testing Count" + count);

}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    To make syn call you need to change this true to false

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/"+ entityname + "$filter=" + entitykey + "eq " + entityid , false);

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    I would suggest that you download CRM Rest Builder from Githum by Jason Lattimer.

    This gives you a solution that enables you to generate Web Api calls, based on particular requests.

    You can specify the type of Web Api call (such as Retrieve, Retrieve multiple, etc..), and specify parameters such as impersonation, asynchronous and synchronous, attributes to return, expand options, and more.

    You can then preview and copy the Web Api JavaScript and paste it in your code file, and preview the results as well.

    Download it from:

    github.com/.../CRMRESTBuilder

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    The synchronous call has been deprecated in most browser using Xmlhttprequest due to browser over head. I would suggest to use  settimeout and wait untill you get the response and then trigger call back method.

  • Srini20 Profile Picture
    397 on at

    Yes.. I read this in an article that it will cause problems in browser and trying to see how can i make my logic work without using settimeout. I am not sure whether using Promise will work. I was looking similar to Aynch and await operation that we use in TypeScript but not sure how to make my asynchronous method get the result to implement the business logic based on Asynchronous result values.

  • Srini20 Profile Picture
    397 on at

    Thanks Aric. I am using this but i was trying to implement this Asynchronous without using Settimeout. Is there any other way i can achieve this using Promise or any other mechanism?

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