D365 Business Central : Set Default Sorting on Page
When designing a page, particularly for Historical Tables, setting a default sorting order is important. But how do we modify this default sorting? Let’s dive into it.
Setting Default Sorting on New Page
To change the default sorting on a new page, we can use the SourceTableView
properties. Here’s an example with a new page:
page 60001 "Test Customer_TNG"
{
ApplicationArea = All;
Caption = 'Test Customer';
PageType = List;
SourceTable = "Customer";
UsageCategory = Lists;
SourceTableView = order(descending);
layout
{...
}
}
As you can see, the default sorting order for this page is set to descending.
![](https://i0.wp.com/thatnavguy.com/wp-content/uploads/2024/05/Default-Sorting-on-Page.jpg?resize=863%2C337&ssl=1)
But what if we want to sort by the ‘Address’ field instead of the ‘No.’ field? We can achieve this by specifying the Sorting
key.
page 60001 "Test Customer_TNG"
{
ApplicationArea = All;
Caption = 'Test Customer';
PageType = List;
SourceTable = "Customer";
UsageCategory = Lists;
SourceTableView = sorting(Address) order(descending);
layout
{ ...
}
}
![](https://i0.wp.com/thatnavguy.com/wp-content/uploads/2024/05/Default-Address-Sorting-on-Page.jpg?resize=997%2C320&ssl=1)
Now, the default sorting is based on the ‘Address’ field. However, remember that sorting fields should be part of the keys, otherwise, you’ll receive a warning.
![](https://i0.wp.com/thatnavguy.com/wp-content/uploads/2024/05/Default-Address2-Sorting-on-Page.jpg?resize=1024%2C224&ssl=1)
Set Default Sorting on Page Extension
What if we need to modify the sorting on an existing page using a page extension? Attempting to use the SourceTableView
property will result in an error.
![](https://i0.wp.com/thatnavguy.com/wp-content/uploads/2024/05/SourceTableView-on-Page-Extension.jpg?resize=870%2C141&ssl=1)
To solve this isse, we can use SetCurrentKey
and Ascending
on the OnOpenPage
trigger.
pageextension 60001 "Customer List_TNG" extends "Customer List"
{
trigger OnOpenPage()
begin
Rec.SetCurrentKey(Name);
Rec.Ascending(false);
end;
}
Result:
![](https://i0.wp.com/thatnavguy.com/wp-content/uploads/2024/05/Sorting-on-Page-Extension.jpg?resize=941%2C392&ssl=1)
And there you have it. You’ve now learned how to set default sorting on both new and existing pages. Hope you learn something from it.