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)

OData query using expand, CRM 2011

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    ram r Profile Picture
    on at

    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.

  • Community Member Profile Picture
    on at

    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?

  • ashlega Profile Picture
    34,477 on at

    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..

  • Andreas Cieslik Profile Picture
    9,267 on at

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

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at

    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.

  • Community Member Profile Picture
    on at

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

  • Community Member Profile Picture
    on at

    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.

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