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();
retrieveParentCustomer.open(“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() {
retrieveParentCustomerCallBack(this);
};
retrieveParentCustomer.send();

}
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 Reply