web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
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

I have the same question (0)

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans