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 :
Customer experience | Sales, Customer Insights,...
Suggested answer

OData WebAPI orderby child property not working?

(0) ShareShare
ReportReport
Posted on by 105

For example, I want to order the returned list based on a property from the child but I'm getting 

{"error":{"code":"0x0","message":"Object reference not set to an instance of an object."}}

This request return me the error when want to get a list of opportunities sorted by the related account's name.

https://xxx.api.crm.dynamics.com/api/data/v9.2/opportunities?$expand=parentaccountid&$top=5&$orderby=parentaccountid/name

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at
    RE: OData WebAPI orderby child property not working?

    Hi Kirk T,

    I tested the request with the URL you provided and get the same error with yours. It seems that this is not supported.

    As a workaround, you could use fetchXML to do this:

      
        
        
        
        
        
        
        
          
        
      
    

    You could add properties you need and change link-type to inner based on your demand.

    this URL encode value is:

      
        
        
        
        
        
        
        
          
        
      
    

    And this request url is :

    https://xxx.api.crm.dynamics.com/api/data/v9.2/opportunities?fetchXml=
      
        
        
        
        
        
        
        
          
        
      
    

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: OData WebAPI orderby child property not working?

    Hi,

    You can also create new custom text field on Opportunity entity and then use calculated field to store the Name of your account.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Kirk T Profile Picture
    105 on at
    RE: OData WebAPI orderby child property not working?

    Bipin,

    So if I do this I would have to make a new custom text field for every fields that I plan to use and be sortable. Is this a good idea to do as it will probably add a lot of fields?

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: OData WebAPI orderby child property not working?

    Hi,

    Agree with you.

    I thought we had the requirement to do ordering based on one field.

    You can use fetchxml in case you don't want to create custom field suggested by steve,

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Kirk T Profile Picture
    105 on at
    RE: OData WebAPI orderby child property not working?

    I just tried it and it doesn't seem to have any effect on the sort order. Specifying orderby with the calculated column in asc or desc or not specifying orderby at all returns the exact same result. The behavior matches with the limitation of calculated field listed on this page docs.microsoft.com/.../calculated-rollup-attributes

    "You can’t use values in calculated columns on a logical value in the same table to sort data returned by a query. Although your query can specify that the results should be ordered using a calculated column, the sort direction will be ignored and will not throw an error."

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: OData WebAPI orderby child property not working?

    Hi,

    So instead of creating Calculated field why don't you try Real Time workflow to set the normal text field value.

    You can also check relationship mapping to map Account Name to your custom text field.

    docs.microsoft.com/.../map-entity-fields

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Kirk T Profile Picture
    105 on at
    RE: OData WebAPI orderby child property not working?

    I thought about that but it turns out the tables that I'm actually trying to customize (Price List Item and Discount List) are both not customizable and I can't add new fields. If you know a workaround that I would appreciate it.

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: OData WebAPI orderby child property not working?

    Hi,

    As per your original request, you wanted to do ordering on Opportunity and Account Entity which is customizable.

    But as you are saying different entities which is not customizable, you should go with fetchxml if are not able to add new fields.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 149

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 132

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 123 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans