Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

odata query

(0) ShareShare
ReportReport
Posted on by 2,215

Hi, I have a such a problem: in my code I want to retrieve customer's field text value (its type is a lookup).

function OnLoad() {

    var serverUrl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();

    var primarycontact = Xrm.Page.getAttribute("customerid").getValue();

    var primarycontactid = primarycontact[0].id;

    var primarycontacttype = primarycontact[0].entityType;

    alert(primarycontacttype);

 

 // Creating the Odata Endpoint

    var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

    var retrieveReq = new XMLHttpRequest();

    if (primarycontacttype == "contact") {

        var Odata = oDataPath + "/ContactSet?$select=new_ownershop_type&$filter=ContactId eq guid'" + primarycontactid + "'";

 

 

        retrieveReq.open("GET", Odata, false);

        retrieveReq.setRequestHeader("Accept", "application/json");

        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

        retrieveReq.onreadystatechange = function () { retrieveReqCallBack1(this); };

    }

   if (primarycontacttype == "account") {

        var Odata = oDataPath + "/AccountSet?$select=new_ownership_type&$filter=AccountId eq guid'" + primarycontactid + "'";

        retrieveReq.open("GET", Odata, false);

        retrieveReq.setRequestHeader("Accept", "application/json");

        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

        retrieveReq.onreadystatechange = function () { retrieveReqCallBack2(this); };

 

    }

    retrieveReq.send();

}

 

function retrieveReqCallBack1(retrieveReq) {

    if (retrieveReq.readyState == 4 /* complete */) {

        var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;

        var EmailAddress = retrieved.results[0].new_ownershop_type.Value;

 

        alert(EmailAddress.name);

    }

}

 

function retrieveReqCallBack2(retrieveReq) {

    if (retrieveReq.readyState == 4 /* complete */) {

        var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;

        var EmailAddress = retrieved.results[0].new_ownership_type.Value;

        alert(EmailAddress.name);

    }

}

 

 new_ownershop_type and new_ownership_type are lookup fields

Thanks!

 

*This post is locked for comments

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
dkrishna Profile Picture

dkrishna 6

Featured topics

Product updates

Dynamics 365 release plans