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);
}