Dynamics 365 Business Central: Using Record.SetView(String) method to set sorting, order, and filters at the same time

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share a mini tip for Business Central, using Record.SetView(String) method to set sorting, order, and filters.

As you might know, we can use Record.Ascending() Method to set the current sort order. And use Record.SetRange() Method or Record.SetFilter() Method to set filters.

For example:

Test Video:

Actually, this can be done using Record.SetView(String) Method as well.
Let’s see more details.

Record.SetView(String) Method: Sets the current sort order, key, and filters on a table.

Syntax: Record.SetView(String: String)

Parameters:
String: A string that contains the sort order, key, and filter to set. The string format is the same as the SourceTableView Property on pages.

When you create a new list page, you can use SourceTableView Property to set the key, sort order, and filter you want to use to determine the view of the source table presented to the user.

PS: The property ‘SourceTableView’ cannot be customized.

So you can do like below. The result is the same.

Sample Code:

pageextension 50101 ZYCustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    begin
        //Rec.SetCurrentKey(Name, "No.");
        //Rec.Ascending(false);
        //Rec.SetFilter("Balance (LCY)", '>%1', 10000);
        //Rec.SetFilter("Sales (LCY)", '<>%1', 0);
        Rec.SetView(StrSubstNo('sorting (Name, "No.") order(descending) where ("Balance (LCY)" = filter (>= %1), "Sales (LCY)" = filter(<> %2))', 10000, 0));
    end;
}

Note:

1. If the SetView method is executed with an empty string, all the filters are removed and the primary key is used.

2. The value of the String parameter can be returned by the RecordRef.GetView([Boolean]) Method

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました