Get Parent Customer ID based on contact id

Requirement: Sometime we have requirement to get contact’s parent customer id.

Solution: We can use REST retrieve method where we can pass record ID and get details from entiy

function ParentCustomerID(ContactId) {

var context = Xrm.Page.context;
var serverUrl = context.getServerUrl(); //Update: in MS CRM 2013 or 2015 you need to use getClientUrl() instead of getServerUrl()
var ODataPath = serverUrl + “/XRMServices/2011/OrganizationData.svc”;
var retrieveParentCustomer= new XMLHttpRequest();“GET”, ODataPath + “/ContactSet(guid'” + ContactId+ “‘)”, false); //Change entity if required
retrieveParentCustomer.setRequestHeader(“Accept”, “application/json”);
retrieveParentCustomer.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
retrieveParentCustomer.onreadystatechange = function() {

function retrieveParentCustomerCallBack(retrieveParentCustomer) {
if (retrieveParentCustomer.readyState == 4 /* complete */) {
if (retrieveParentCustomer.status == 200) {

var retrievedParent = this.parent.JSON.parse(retrieveParentCustomer.responseText).d;

alert(retrievedParent.ParentCustomerId.Id); //for id
alert(retrievedParent.ParentCustomerId.Name); //for name



Enjoy !!!


Leave a comment

Filed under MS CRM 2011

Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s