Reading related records using OData and Jscript – CRM 2011
I have 1:N relationship between “Contact” entity and custom entity “Bikes” (i.e., 1 contact can have N no of Bikes).
If I want to read all “Bikes” along with my “Contact” information using OData service, I can use the “$expand” keyword of OData protocol.
Below is the generic function to prepare the OData URL and make a Asynchronous service call
function retrieveMultiple(odataSetName, select, filter, expand, successCallback) {
var serverUrl = Xrm.Page.context.getServerUrl();
var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;
var odataUri = serverUrl + ODATA_ENDPOINT + “/” + odataSetName + “?”;
if (select) {
odataUri += “$select=” + select;
}
if (expand) {
odataUri += “&” + “$expand=” + expand;
}
if (filter) {
odataUri += “&” + “$filter=” + filter;
}
$.ajax({
type: “GET”,
contentType: “application/json; charset=utf-8”,
datatype: “json”,
url: odataUri,
beforeSend: function (XMLHttpRequest) {
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 (XmlHttpRequest && XmlHttpRequest.responseText) {
alert(“Error while retrieval of ” + odataSetName + ” ; Error – ” + XmlHttpRequest.responseText);
}
}
});
}
How do I use above function
- Consider my “Contact” & “Bike” scenario and set required the columns and “expand” option and call the above “retrieveMultiple” function
- Below is the function to define properties
function retrieveBikesByContactId() {
// Pass ‘Contact’ set name
var oDataSetName = “ContactSet”;
// Set Contact GUID
var contactId = {Contact GUID};
// Prepare filter
var filter = “ContactId eq guid'” + contactId + “‘”;
// Set expand (i.e., Relationship name of ‘Contact’ and ‘Bike’)
var expand = “honda_contact_honda_bikeinformation”;
// Column names of ‘Contact’ and ‘Bikes’
// Since Bike is related entity specify Bike columns as (Relationship name\column name) (i.e., honda_contact_honda_bikeinformation/honda_name)
var columns = “FullName,honda_contact_honda_bikeinformation/honda_name,honda_contact_honda_bikeinformation/honda_Year”;
retrieveMultiple(oDataSetName, columns, filter, expand, readDataOnSuccess);
}
- After the success of data retrieval, parse the result using “readDataOnSuccess” method
function readDataOnSuccess(data, textStatus, XmlHttpRequest) {
if (data && data.length > 0) {
for (var indx = 0; indx < data.length; indx++) {
if (data[indx].honda_contact_honda_bikeinformation) {
// Read the child entity records using “expand.results” (i.e., honda_contact_honda_bikeinformation.results)
var childResults = data[indx].honda_contact_honda_bikeinformation.results;
for (var indxChild = 0; indxChild < childResults.length; indxChild++) {
alert(childResults[indxChild].honda_name);
}
}
}
}
}
Use the “OData Query Designer” tool to get the “expand” name and column names
- To get related entity record fields, click “One To Many” tab and choose the relationship
Stats
- 1,607,893 hits
Top Posts
- [Step by Step] Power Apps | Show pop ups in Canvas App
- Power Apps component framework (PCF) - Beginner guide
- [Step by Step] Connecting to Azure SQL Server using OLEDB Connection from SSIS
- Auto generate new GUID for ‘uniqueidentifier’ column in SQL Table
- Power Platform - Pass json collection from Canvas App to Power Automate
- Power Automate Cloud Flow | 'Correct to include a valid reference' error
- [Step by Step] Postman tool with Microsoft Dataverse Web API
- [Code Snippet] Set Business process flow (BPF) stage using C#
- [Code Snippet] Custom Workflow Activity with Input and Output Params
- [Step by Step] Power Apps Portal - Configure Global Search