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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

OData Query not working

(0) ShareShare
ReportReport
Posted on by

I have my query built to pull contact information from the contact form and populate it on the Ticket form. Here is my query:

function getContactDetails()
{
var lookUpObjectValue = Xrm.Page.getAttribute("new_customer").getValue();
if ((lookUpObjectValue != null))
{
var lookuptextvalue = lookUpObjectValue[0].name;

var lookupid = lookUpObjectValue[0].id;
//alert(lookupid);


var serverUrl = Xrm.Page.context.getServerUrl();

//The XRM OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";


var odataSetName = "ContactSet";

var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + lookupid + "')";

//alert(odataSelect);

$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: odataSelect,
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
success: function (data, textStatus, XmlHttpRequest) {

var result_contact= data.d;

//alert(result_contact.firstname);
var mc_phone1 = result_contact.telephone1;
//replace the fields with the fields on your entity
Xrm.Page.getAttribute("new_phone").setValue(mc_phone1);
Xrm.Page.getAttribute("new_email").setValue(result_contact.emailaddress1);
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
});

}

}

It runs with out error and gets to the OdataSelect part and you can hit the page and it has the information I need to pull but when it goes through the rest of the query starting at the $.ajax part it is not pulling the data I need and populating it on the form. Am I missing something that is preventing this from working? 

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: OData Query not working

    Thank you Chitra

    Harihar

  • Royal King Profile Picture
    27,686 on at
    RE: OData Query not working

    Just copy the url that you generated and paste url in the browser  then hit enter to see the result of the odata query. As odata is a rest based api you should be able to run all odata query using any broswser.

  • Community Member Profile Picture
    on at
    RE: OData Query not working

    High level hints please to try out on the trial on-line version.

    Harihar

  • Community Member Profile Picture
    on at
    RE: OData Query not working

    Could one of you share /point me on how to test the above - high level steps please. I have an on-line 2013 version - evaluating the application for our needs. Helps me to understand the functionality better.

    Thank you for your insight.

    Harihar

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: OData Query not working

    Hello,

    Your script looks fine and I just tested it I am getting result, you just need to change two things

    Xrm.Page.context.getServerUrl() -> Xrm.Page.context.getClientUrl()

    And while working with data you need to use Schema name of the fields so for example for below line

    var mc_phone1 = result_contact.Telephone1;  //schema name of telephone1 is Telephone1

    And make sure you are using correct fields name

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#2
Christoph Pock Profile Picture

Christoph Pock 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans