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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

XMLHttpRequest odata query help please

(0) ShareShare
ReportReport
Posted on by 7,316
HI,

we've just upgraded to CRM 2016 from 2015 on prem. This below script was working fine until 2015.

I've modified the script to use the new api format. But the XMLHttprequest returns 400 no matter what changes I do.

I've built the odata query  from CRM Rest builder tool in which I don't have option for adding filters. So, the filter part was written by me and I am not sure if the filter is the issue or something else. Can somebody please look into this and guide me? Thanks.


function countryNumber(countryId) { var serverUrl = Xrm.Page.context.getClientUrl(); var ODataPath = serverUrl + "/api/data/v8.0/new_countries?$select=new_countrynumber&$filter=new_countryId eq (guid\'" + countryId + "\')"; var userRequest = new XMLHttpRequest(); userRequest.open("GET", encodeURI(ODataPath), false); userRequest.setRequestHeader("OData-MaxVersion", "4.0"); userRequest.setRequestHeader("OData-Version", "4.0"); userRequest.setRequestHeader("Accept", "application/json"); userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); userRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); userRequest.send(); if (userRequest.status === 200) { var retrievedUser = JSON.parse(userRequest.responseText); if (retrievedUser != null && retrievedUser.results.length > 0) { var countryNumber = retrievedUser.results[0].new_countrynumber; return countryNumber; } } }


 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    ashlega Profile Picture
    34,477 on at

    Hi,

     have a look at this:

    "Filter records based on single-valued navigation property"

    msdn.microsoft.com/.../gg334767.aspx

    Your "path" should probably look more like this:

    var ODataPath = serverUrl + "/api/data/v8.0/new_lapcounties?$select=new_countynumber&$filter=new_lapcounty/new_lapcountyid eq " + countyId;

    You don't need that guid' etc, btw

  • Suggested answer
    SavvyLuke Profile Picture
    655 on at

    Hi there,

    Save yourself the headache and install the CRM Rest builder tool.

    Then you can see what you're doing wrong.

  • Suggested answer
    Nuno Profile Picture
    2,005 on at

    var ODataPath = serverUrl + "/api/data/v8.0/new_lapcounties?$select=new_countynumber&$filter=new_lapcountyid eq '" + countyId + "'";

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans