Retrieve Related entity’s field value from client side using oData

Below is the sample of how to retrieve related entity’s field value using oData.

Let’s take a example ..suppose we want to retrieve some field value from SystemUser entity(User Name and EmployeeId) OnLoad of Account where OwnerId of Account is equal to SystemUserId of SystemUser entity.Write below  code on OnLoad of Account entity.

var serverUrl=Xrm.Page.context.getServerUrl();
var ownerId=Xrm.Page.getAttribute(“ownerid”).getValue()[0].id;
var ownerGuid=ownerId.substring(1,ownerId.length-1);

var oDataQuery=serverUrl+”/xrmservices/2011/OrganizationData.svc/SystemUserSet?$select=DomainName,EmployeeId,ParentSystemUserId,AccessMode&$expand=user_accounts&$filter=SystemUserId eq guid'”+ownerGuid+”‘”;

$.ajax(

{
type: “GET”,
contentType: “application/json; charset=utf-8”,
datatype: “json”,
url: oDataQuery,
beforeSend: function(XMLHttpRequest)
{
XMLHttpRequest.setRequestHeader(“Accept”,”application/json”);
},
success: function(data, textStatus, XmlHttpRequest)
{
if(data != null && data.d != null && data.d !=null)
{
var users=data.d.results[0];
var userName=users.DomainName;

//Lookup Manager(ParentSystemUserId)
var managerName=users.ParentSystemUserId.Name;
var managerId= users.ParentSystemUserId.Id;
var managerType=users.ParentSystemUserId.LogicalName;

// OptionSet
var accessMode=users.AccessMode.Value // This will return integer value

var employeeId=users.EmployeeId;
alert(‘User Name : ‘+users.DomainName +’Employee Id :’ +users.EmployeeId);
}
},
error: function(XmlHttpRequest,textStatus,errorThrown)
{
alert(‘oData select failed ‘+ oDataQuery);
}
}
);

Note : don’t forget to load json2.js and jquery1.4.1.min.js  framework on Account Form.

Hope it will help u.

Author: Arvind Singh

Microsoft Dynamics CRM Consultant

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s