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

  • Suggested answer
    Nuno Profile Picture
    Nuno 2,005 on at
    RE: XMLHttpRequest odata query help please

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

  • Suggested answer
    SavvyLuke Profile Picture
    SavvyLuke 655 on at
    RE: XMLHttpRequest odata query help please

    Hi there,

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

    Then you can see what you're doing wrong.

  • Verified answer
    ashlega Profile Picture
    ashlega 34,477 on at
    RE: XMLHttpRequest odata query help please

    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

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,387 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans