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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to sort on a reference data source field in a grid

(0) ShareShare
ReportReport
Posted on by

Hello,

I have a form grid that displays data from my form data source and a reference data source.  I'd like to sort the grid on a reference data source field.  My data source has a RecId that references the main account table and I'd like to sort my form on the MainAccount.MainAccountId.  Is this possible?  Users can click the MainAccountId column heading to sort the grid this way.  If it were a normal data source, I would write code in the form data source init method to get a handle on the data source and specify the sort field like this: this.queryBuildDataSource().addSortField(...)

However, I'm struggling to find a way to get a handle on the reference data source and there is no init method to override for the reference data source.  Any suggestion would be greatly appreciated!  

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hi,

    You can go to the Init Method from the datasource that you want to add the filters.

    Create the query relation like this:

    public void init()

    {

       str itemId;

       itemId = element.args().parm();

       super();

       this.query().dataSourceTable(tableNum("ItemInventory")).addRange(fieldNum(ItemInventory,ItemId)).value(itemId);

    }

    In this case the value to filter must to be know it.

    but if you want to use a dynamic value in a filter field in the header form, then add the code in the executeQuery method in the datasource, and call this method in the modified method in the filter field.

    Thanks!

  • Greg's Mom Profile Picture
    on at

    Thanks for the suggestion, Tatiana.  I have tried this approach and the problem I'm facing is that I'm trying to sort on a reference data source and the code: 

    this.query().dataSourceTable(tableNum("MyReferenceTable")) 

    This returns null.  I'm able to get a handle on and sort/filter on a regular data source but the reference data source is returning null.

  • Community Member Profile Picture
    on at

    what is the relation between the tables?

    I guess you have both tables as a datasuorces and set the relation between those.

  • Greg's Mom Profile Picture
    on at

    >> what is the relation between the tables?

    MyTable.MyReferenceTable = MyReferenceTable.RecId

    >> I guess you have both tables as a datasuorces and set the relation between those.

    MyTable is a data source and MyReference table is added as a reference data source under MyTable.  

  • startax Profile Picture
    1,845 on at

    Hi Brawndo,

    Just look the from "HcmWorkerListPage" and add a new reference field in grid from DirPerson table. No need to write any code for sorting the grid.

  • Greg's Mom Profile Picture
    on at

    Thanks for the suggestion, Dinkar.  Are you saying to use a Query as the form data source?  I'm looking at the HcmWorkerListPage and I'm struggling a little bit to understand your suggestion.  Maybe I should explain my requirement a little more clearly. I have a table called BudgetWorksheet which has a column called MainAccount which relates to the RecId column in the MainAccount table.  I have a form which displays the budget worksheet accounts and some fields allowing users to enter budget amounts for each account.  My form has the BudgetWorksheet table as the main data source and the MainAccount table as a reference data source so that I can display the MainAccountId field on the form.  

    • Forms\BudgetWorksheet
      • Data Sources
        • BudgetWorksheet
          • Methods
          • Fields
          • Reference Data Sources
            • MainAccount
      • Designs
        • Design
          • Grid
            • MainAccount_MainAccountId
            • BudgetWorksheet_BudgetAmount

    I'd like to sort my form on the MainAccountId.  If I stored the MainAccountId column were in the BudgetWorksheet table then I could write code like Tatiana had mentioned above.  However, when I try to get a handle on the MainAccount reference data source, I get null so I cannot add the sort.  The problem is that the MainAccountIds are not created in a sequential order so sorting on the MainAccount RefRecId in my BudgetWorksheet table doesn't give me the sort that I'm looking for.  I need to either include MainAccountId in my table or find another way to sort this.

  • Greg's Mom Profile Picture
    on at

    I'm wondering if any AX experts out there can confirm whether it is possible in AX 2012 R2 to sort a grid in code from a column in a reference data source?  I'm frustrated that this doesn't seem doable as I am able to click on the column heading and sort the grid while the form is running but I'm unable to get a handle on the reference data source in the code and apply the sort automatically.  As far as I can tell, my options are to not give my user the automatic sort they requested or I can denormalize the data and duplicate my reference data source data in my table to be able to automatically apply the sort.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 21

#2
dserp Profile Picture

dserp 4

#2
dekion Profile Picture

dekion 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans