Dynamics 365 Business Central: How to get a filter for the selected records on any page (For example, ‘1..3|6’)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to get a filter for the selected records on any page, for example, ‘1..3|6’.

What does it mean? Let’s see an example.
There is an Account Filter field on the Analysis View Card page.

When you select multiple G/L accounts, it automatically turned into a filter.

Test Video:

How is this done? Let’s look again at the standard code.
GLAccList.GetSelectionFilter;

In the end we will find codeunit 46 SelectionFilterManagement.

Get a filter for the selected field from a provided record. Ranges will be used inside the filter were possible.

If you look at its source code, you will see that Microsoft has provided standard functionality for many tables.

For example, for Item, we can call the standard method directly. (The standard method is generally saved in the list page)

Source Code:

pageextension 50101 CustomerListExt extends "Customer Card"
{
    layout
    {
        addafter(Name)
        {
            field(ItemFilter; ItemFilter)
            {
                Caption = 'Item Filter';
                ApplicationArea = All;

                trigger OnLookup(var Text: Text): Boolean
                var
                    ItemList: Page "Item List";
                begin
                    Clear(ItemFilter);
                    ItemList.LookupMode(true);
                    if ItemList.RunModal() = Action::LookupOK then begin
                        Text += ItemList.GetSelectionFilter();
                        exit(true);
                    end else
                        exit(false);
                end;
            }
        }
    }
    var
        ItemFilter: Code[250];
}

For other tables, we can use procedure GetSelectionFilter in codeunit 46 SelectionFilterManagement.

Let’s look at another example, say we want to get a filter for selected Posted Sales Invoices.

Test Video:

Source Code:

pageextension 50101 CustomerListExt extends "Customer Card"
{
    layout
    {
        addafter(Name)
        {
            field(PostedSIFilter; PostedSIFilter)
            {
                Caption = 'Posted SI Filter';
                ApplicationArea = All;

                trigger OnLookup(var Text: Text): Boolean
                var
                    PostedSalesInvocies: Page "Posted Sales Invoices";
                    SalesInvHeader: Record "Sales Invoice Header";
                    SelectionFilterManagement: Codeunit SelectionFilterManagement;
                    RecRef: RecordRef;
                begin
                    Clear(PostedSIFilter);
                    PostedSalesInvocies.SetTableView(SalesInvHeader);
                    PostedSalesInvocies.LookupMode(true);
                    if PostedSalesInvocies.RunModal = ACTION::LookupOK then begin
                        PostedSalesInvocies.SetSelectionFilter(SalesInvHeader);
                        RecRef.GetTable(SalesInvHeader);
                        PostedSIFilter := SelectionFilterManagement.GetSelectionFilter(RecRef, SalesInvHeader.FieldNo("No."));
                    end;
                end;
            }
        }
    }
    var
        PostedSIFilter: Code[250];
}

PS:
1. There is one thing to note here. When using this method, you must ensure that the record is sorted by the field you want to get. If it is ascending, the filter of ‘..’ can be taken. If it is descending, the system will consider it as a single record, all separated by ‘|’. And try not to have duplicate values. If the sorting is faulty, the wrong filter may be taken.

2. As with the standard, the filter field should be set Code[250].

Okay, the next question is, if we want to get the filter directly for the selected records on the opening page, is it possible?

Of cource, let’s look at two more examples. (Not good examples, but just to know the usage)

1. Get a filter (Field: Code) on the Payment Terms page.

Source Code:

pageextension 50102 PaymentTermsExt extends "Payment Terms"
{
    actions
    {
        addfirst(processing)
        {
            action(GetFilterForSelectedRecords)
            {
                Caption = 'Get Filter For Selected Records';
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                PromotedOnly = true;
                ApplicationArea = All;
                Image = GetOrder;

                trigger OnAction()
                var
                    PaymentTerms: Record "Payment Terms";
                    SelectionFilterManagement: Codeunit SelectionFilterManagement;
                    RecRef: RecordRef;
                begin
                    PaymentTerms.Reset();
                    CurrPage.SetSelectionFilter(PaymentTerms);
                    RecRef.GetTable(PaymentTerms);
                    Message(SelectionFilterManagement.GetSelectionFilter(RecRef, PaymentTerms.FieldNo(Code)));
                end;
            }
        }
    }
}

2. Get a filter (Fields: No. or Sell-to Customer No.) on the Sales order list page.

Test Video:

Source Code:

pageextension 50103 SalesOrderExt extends "Sales Order List"
{
    actions
    {
        addfirst(processing)
        {
            action(GetFilterForSelectedRecords)
            {
                Caption = 'Get Filter For No.';
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                PromotedOnly = true;
                ApplicationArea = All;
                Image = GetOrder;

                trigger OnAction()
                var
                    SalesHeader: Record "Sales Header";
                    SelectionFilterManagement: Codeunit SelectionFilterManagement;
                    RecRef: RecordRef;
                begin
                    SalesHeader.Reset();
                    CurrPage.SetSelectionFilter(SalesHeader);
                    RecRef.GetTable(SalesHeader);
                    Message(SelectionFilterManagement.GetSelectionFilter(RecRef, SalesHeader.FieldNo("No.")));
                end;
            }
            action(GetFilterForSelectedRecords2)
            {
                Caption = 'Get Filter For Customer No.';
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                PromotedOnly = true;
                ApplicationArea = All;
                Image = GetOrder;

                trigger OnAction()
                var
                    SalesHeader: Record "Sales Header";
                    SelectionFilterManagement: Codeunit SelectionFilterManagement;
                    RecRef: RecordRef;
                begin
                    SalesHeader.Reset();
                    CurrPage.SetSelectionFilter(SalesHeader);
                    RecRef.GetTable(SalesHeader);
                    Message(SelectionFilterManagement.GetSelectionFilter(RecRef, SalesHeader.FieldNo("Sell-to Customer No.")));
                end;
            }
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

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