Hello CRM Lovers,
First of all thanks for reading my post. As we all know that after CRM Roll-Up 12, SOAP has stopped working for cross browser support in CRM, so we have to write some other code to solve this problem. I found a way to solve this problem so I am sharing it with the beautiful world.
In this post I am going to describe how to retrieve data with java script of another entity in CRM 2011, CRM 2013 with JSON and OData.It will work on Roll-Up 12, Roll-Up 13 for CRM 2011 and on CRM 2013, and also in cross browsers. It will really help you. For retrieving data please fallow these steps.
Retrieving Multiple Records with ODATA in CRM with Java Script
Java script for retrieving data in cross browser
//***********************************************************************************************************************
//*******Retrive Multiple recored on the basis of the criteria OData Rest //******************************************************************************************
function retrieveMultiple(odataSetName, filter, successCallback, errorCallback, _executionObj) {
_executionObjMultiretrive = _executionObj;
var context = Xrm.Page.context;
var serverUrl = context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Build the URI
var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName;
//If a filter is supplied, append it to the OData URI
if (filter) {
odataUri += filter;
}
//Asynchronous AJAX function to Retrieve CRM records using OData
$.ajax({
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json", url: odataUri,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
if (data && data.d && data.d.results) {
successCallback(data.d.results, textStatus, XmlHttpRequest);
}
else if (data && data.d) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
else {
successCallback(data, textStatus, XmlHttpRequest);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback)
errorCallback(XmlHttpRequest, textStatus, errorThrown);
else
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
});
}
function errorHandler(xmlHttpRequest, textStatus, errorThrow) {
alert("Error : " + textStatus + ": " + xmlHttpRequest.statusText);
}
//**********************************************************************************//*****************Your custom function for retrieving data********************
//**********************************************************************************
function YourFunction() {
var _condition
= "multiple retrieve with odata";
if (_condition!= null) {
retrieveMultiple("Entity NameSet", "?$filter=
Attribute Schema Name eq '"+_condition+"' ", RetrieveEntityRecords, null, null);
}
}
//****************************************************************//****************************************************************
function RetrieveEntityRecords(data, textStatus, XmlHttpRequest) {
//debugger;
if (data.length > 0) {
for (i = 0; i < data.length; i++) {
var attribute1 = data[i].attribute1 schema name;
var attribute2 = data[i].attribute2 schema name;
}
}
}
Here "data" is object of all retrieved records, you can find your required attribute accordingly.
I know your condition may be differ.
The filter condition differs on the basis of attribute type so you can visit this link for more conditions...