Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

OData query using expand, CRM 2011

Posted on by Microsoft Employee

I'm trying to query the Opportunity set, and I have a custom lookup field on the opportunity form that I'm trying to get the value of (either the guid or the name - preferably the name). However, I'm having trouble writing the expand part of it, as I haven't used that command before. Here's what I have so far:

var ODATA_EntityCollection = "/OpportunitySet";
var urlQuery = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection;
urlQuery += "?$select=new_Type,new_Date,new_Category,OpportunityId,new_opportunity_Agent&$expand=new_opportunity_Agent&$filter=CustomerId/Id eq (guid'" + contactId + "') and StatusCode/Value eq 1&$orderby=new_Date asc";

var req = new XMLHttpRequest();
req.open("GET", urlQuery, false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.send(null);

var returned = eval('(' + req.responseText + ')').d;				
var results = returned.results;
alert("This doesn't show up");
if (results == null || (results != null && results.length == 0))
	return null;
return results;

It doesn't get to the alert. Any ideas?

This was working fine before I tried the expand.

Thanks.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: OData query using expand, CRM 2011

    Thanks for that. I imported the solution, but I don't get anything in the 'Primary Entity' drop down. It looks like it's not working for me.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: OData query using expand, CRM 2011

    It works with the expand. I get back 1 result. It's not in xml format, just an RSS feed.

  • Suggested answer
    Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 on at
    RE: OData query using expand, CRM 2011

    Are you using this tool:

    github.com/.../CRMRESTBuilder

    it could help you to get the right query and JS Syntax.

    Secondly I would debug to this line:

    var returned = eval('(' + req.responseText + ')').d;

    I doubt this works correct.

  • Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 on at
    RE: OData query using expand, CRM 2011

    Does the URL work in the browser without the expand part?

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: OData query using expand, CRM 2011

    Hi,

     did you try to debug? At least you might add an "alert" right after send to see what's returned from the server.. I'm wondering if this is because of the synchronous Http request, although, since you are on 2011.. it's probably and older version of IE anyway, so should be supported..

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: OData query using expand, CRM 2011

    Thanks for your reply. I have checked the relationship name, and it is correct. I also copied the URL into a browser and that also works. Is the construction of the rest of the code ok?

  • Suggested answer
    ram r Profile Picture
    ram r on at
    RE: OData query using expand, CRM 2011

    Check the relationship name is valid and in proper casing(new_opportunity_Agent), that might solve the issue. The better way is to hit the url in browser and test before proceeding to JS.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,219 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans