Hi,
Here is the JavaScript code for retrieving records from CRM using OData Synchronously.
function retrieveContactsByAccountId(accountId) {
// Pass 'Contact' set name since we are reading Contacts
var oDataSetName = "AccountSet";
// Column names of 'Contact' (Pass * to read all columns)
var columns = "Name";
// Prepare filter
var filter = "AccountId eq guid'" + accountId + "'";
var requestResults = retrieveMultipleSync(oDataSetName, columns, filter);
var account = null;
if (requestResults != null) {
if (requestResults.results != null && requestResults.results.length > 0) {
for (var indxContacts = 0; indxContacts < requestResults.results.length; indxContacts++) {
account = requestResults.results[indxContacts];
if (account.Name) {
alert(account.Name);
}
}
}
}
else {
alert("An error has occured. " + service.responseText);
}
}
// Retrieve Multiple Synchronous Operation.
function retrieveMultipleSync(odataSetName, select, filter) {
// Get Server URL
var serverUrl = "";
if (Xrm.Page.context.getClientUrl) {
//Post UR 12
serverUrl = Xrm.Page.context.getClientUrl();
}
else {
//Pre UR 12
serverUrl = Xrm.Page.context.getServerUrl();
}
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "?";
if (select) {
odataUri += "$select=" + select;
}
if (filter) {
odataUri += "&" + "$filter=" + filter;
}
var service = GetRequestObject();
if (service != null) {
service.open("GET", odataUri, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json,text/javascript, */*");
service.send(null);
var requestResults = eval('(' + service.responseText + ')').d;
return requestResults;
}
}
return new window.XMLHttpRequest;
}
else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (ex) {
return null;
}
}
}
--
Happy CRM'ing
Gopinath
Here is the JavaScript code for retrieving records from CRM using OData Synchronously.
function retrieveContactsByAccountId(accountId) {
// Pass 'Contact' set name since we are reading Contacts
var oDataSetName = "AccountSet";
// Column names of 'Contact' (Pass * to read all columns)
var columns = "Name";
// Prepare filter
var filter = "AccountId eq guid'" + accountId + "'";
var requestResults = retrieveMultipleSync(oDataSetName, columns, filter);
var account = null;
if (requestResults != null) {
if (requestResults.results != null && requestResults.results.length > 0) {
for (var indxContacts = 0; indxContacts < requestResults.results.length; indxContacts++) {
account = requestResults.results[indxContacts];
if (account.Name) {
alert(account.Name);
}
}
}
}
else {
alert("An error has occured. " + service.responseText);
}
}
// Retrieve Multiple Synchronous Operation.
function retrieveMultipleSync(odataSetName, select, filter) {
// Get Server URL
var serverUrl = "";
if (Xrm.Page.context.getClientUrl) {
//Post UR 12
serverUrl = Xrm.Page.context.getClientUrl();
}
else {
//Pre UR 12
serverUrl = Xrm.Page.context.getServerUrl();
}
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "?";
if (select) {
odataUri += "$select=" + select;
}
if (filter) {
odataUri += "&" + "$filter=" + filter;
}
var service = GetRequestObject();
if (service != null) {
service.open("GET", odataUri, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json,text/javascript, */*");
service.send(null);
var requestResults = eval('(' + service.responseText + ')').d;
return requestResults;
}
}
function GetRequestObject() {
if (window.XMLHttpRequest) {return new window.XMLHttpRequest;
}
else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (ex) {
return null;
}
}
}
--
Happy CRM'ing
Gopinath
No comments:
Post a Comment