How to sort data on grid of form in AX 2012
In Microsoft Dynamics AX 2012 and Microsoft Dynamics 365 for Finance and Operations, grids are used very often on the form. If you want to sort data on grid of form in AX 2012 by ascending or descending orders. You are at the right place to learn.
The requirement is that I want to sort data on grid of form in AX 2012 when the form is opened. After successfully fulfilling the requirement in Microsoft Dynamics AX 2012, I planned to share my experience.
So, if you want to sort data on grid of form in Microsoft Dynamics AX 2012, you need to write two lines in the data source’s init() method after super() is called. Before starting, you should know the following:
Sr. # | Element | Remarks about the the element |
1. | Form | Form which contains the grid on which you want to apply sorting |
2. | Field | Field in data source of form on which you want to apply sorting |
3. | Data source | Data source in form which contains the field on which you want to apply sorting |
4. | Table | Table used in the data source of form |
In my case, I have used the following:
Sr. # | Element | Element name in AOT |
1. | Form | SysDatabaseLog |
2. | Field | createdDateTime |
3. | Data source | SysDatabaseLog |
4. | Table | SysDatabaseLog |
To implement the solution to sort data on grid of form in AX 2012, follow these steps:
- Open the development work space
- Go to AOT -> Forms -> FORM -> DATA SOURCE -> Methods
- Override the init() method of data source if it does not exist, or, open the existing init() method
- After super(), add the following code:
//Moeen Ahmed Sultan
//It will clear if any other sorting is applied on the grid
this.queryBuildDataSource().sortClear();
//It will add the sorting on the field
//Use the table and field as per your own requirement in fieldNum function.
//Use the ascending or descending order as per your own requirement
this.queryBuildDataSource().addSortField(fieldNum(SysDataBaseLog,CreatedDateTime),SortOrder::Descending);
Using the above two lines, you will be able to get the sorted data when the form is opened. In this way, you will achieve your requirement that is sort data on grid of form in AX 2012.
Blog: Click here
YouTube: Click here
GitHub: Click here
If you found any ambiguity or better solution, please let me know. If this helps you, please like, comment and share.
Dreams don’t work, unless you do.