/ Microsoft / How to sort data on grid of form in D365FO using X++

How to sort data on grid of form in D365FO using X++

Learn, share and spread knowledge

Sorting data on grid of form in Microsoft Dynamics 365 for Finance and Operations and Microsoft Dynamics AX 2012 can be done through the form. But every time you have to do it whenever you will open the form.
To sort the data on grid of form in D365FO whenever form is opened can be achieved using extension approach of development.

I have shared the article in which I have described How to sort data on grid of form in Microsoft Dynamics AX 2012 using X++. But, in Microsoft Dynamics 365 for Finance and Operations, it is different as you have to use the events and extension approach instead of directly over layering the code of the Microsoft.

If you read my article about how to sort data on grid of form in Microsoft Dynamics AX 2012 using X++, you will come to know the prerequisites. Only the difference is writing the code. In D365FO, we have to create an extension class and copy the event handler method of OnInitialized event of the data source and paste the event handler method in the class. And then writing the two lines of code.

Before starting the customization about how to sort data on grid of form in D365FO, you need to know the names of the following elements you are going to use:

Sr. #ElementRemarks about the the element
1.FormForm which contains the grid on which you want to apply sorting
2.FieldField in data source of form on which you want to apply sorting
3.Data sourceData source in form which contains the field on which you want to apply sorting
4.TableTable used in the data source of form

This above table is already described in the article: How to sort data on grid of form in Microsoft Dynamics AX 2012 using X++.

In my case, I am using the following elements as I have already used them in AX 2012:

Sr. # ElementElement name in AOT
1.FormSysDatabaseLog
2.FieldcreatedDateTime
3.Data source
SysDatabaseLog
4.TableSysDatabaseLog

This above table is already described in the article: How to sort data on grid of form in Microsoft Dynamics AX 2012 using X++.

Till now, all the things are same as I have done in the Microsoft Dynamics AX 2012. From now onward, the customization is different from AX 2012.

Let’s focus!

To implement the solution to sort data on grid of form in D365FO, follow these steps:

  • Run the Visual Studio as Administrator
  • Create model, package and project or use existing project
  • Go to AOT -> Forms -> FORM -> DATA SOURCE -> Events
  • Right click the OnInitialized event of data source and click Copy event handler method
  • Create a new class and name it as: NameOfForm_Extension
  • Make the class public and static
  • Paste the event handler we have copied above in the newly created class
  • Add the following two lines of code in the OnInitialized event handler:
//Moeen Ahmed Sultan
//It will clear if any other sorting is applied on the grid
sender.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
sender.queryBuildDataSource().addSortField(fieldNum(SysDataBaseLog,CreatedDateTime),SortOrder::Descending);
  • Build and synchronize the project
  • Run the form through front end and see the effect.

Using the extension approach, creating a class and pasting the event handler and adding the two lines of code, you will be able to get the sorted data when the form is opened. In this way, you will achieve your requirement that is to sort data on grid of form in D365FO.

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.

Don’t let mistakes and disappointments of the past control and direct your future. – Zig Ziglar


1 POST COMMENT

Send Us A Message Here

Your email address will not be published. Required fields are marked *

1 Comments