Dynamics 365 Business Central: How to hide selected records on a list page (Exclusion filtering)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about an interesting question, how to hide selected records on a list page (Exclusion filtering).
What does it mean? As you know, we can use Page.SetSelectionFilter(var Record) Method to get the record that that the user has selected on the page. Of course, it is not difficult to filter only the records selected by the user. For example, on the Posted Sales Invoices (143, List):

Test video:

In addition, we can also save the selected record as a filter text. More details: Dynamics 365 Business Central: How to get a filter for the selected records on any page (For example, ‘1..3|6’)

So can we do the opposite? Hide selected records from the page. This is a little difficult. When we enter Criteria in Filters, we can combine various format expressions, for example,
103182..103188|103198|103145

But When we use not equal to multiple criteria, we cannot directly use the expressions below.
<>103182..103188|103198|103145

The filter “<>103182..103188|103198|103145” is not valid for the No. field on the Sales Invoice Header table. The left side of a range (‘..’) must be a value or empty.

We need to use the Not equal symbol multiple times. More details: Dynamics 365 Business Central: Entering Criteria in Filters (Not equal to multiple criteria)
For example, <>103182&<>103183&<>103184&<>103185&<>103186&<>103187&<>103188&<>103198&<>103145

So it needs to be handled that way in the code as well. Let’s look at a simple example:

Great.

Test video: When the content in the filter is greater than a certain amount, the system will automatically add brackets, but this does not affect the results.

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50202 PostedSalesInvoicesExt extends "Posted Sales Invoices"
{
    actions
    {
        addbefore("Update Document")
        {
            action(FilterSelected)
            {
                ApplicationArea = All;
                Caption = 'Filter Selected';
                Image = Filter;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;

                trigger OnAction()
                begin
                    CurrPage.SetSelectionFilter(Rec);
                    CurrPage.Update();
                end;
            }
            action(ResetFilter)
            {
                ApplicationArea = All;
                Caption = 'Reset Filter';
                Image = Filter;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;

                trigger OnAction()
                begin
                    Rec.Reset();
                    CurrPage.Update();
                end;
            }
            action(ExclusionFilter)
            {
                ApplicationArea = All;
                Caption = 'Exclusion Filter';
                Image = Filter;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;

                trigger OnAction()
                var
                    SalesInvoiceHeader: Record "Sales Invoice Header";
                begin
                    Rec.Reset();
                    SalesInvoiceHeader.Reset();
                    CurrPage.SetSelectionFilter(SalesInvoiceHeader);
                    Rec.SetFilter("No.", GetExclusionFilter(SalesInvoiceHeader));
                    CurrPage.Update();
                end;
            }
        }
    }
    local procedure GetExclusionFilter(var SalesInvoiceHeader: Record "Sales Invoice Header"): Text
    var
        Filter: Text;
    begin
        Filter := '';
        if SalesInvoiceHeader.FindSet() then
            repeat
                if Filter = '' then
                    Filter := '<>' + Format(SalesInvoiceHeader."No.")
                else
                    Filter := Filter + '&' + '<>' + Format(SalesInvoiceHeader."No.");
            until SalesInvoiceHeader.Next() = 0;
        exit(Filter);
    end;
}

PS: There is actually another way to consider this, using a temporary table, deleting the selected records, and then reopening the page using this temporary table. But I personally don’t find it as simple as that.

Let’s look at another example, hiding the selected records so that it will not be displayed when the page is opened again. This is also the question that was asked. When we discussed Dynamics 365 Business Central: Can we delete Posted Documents (Posted invoices and credit memos)???, we mentioned that posted documents can be deleted, but this is not recommended. So is there a way to hide it? This is a solution similar to standard Security Filters feature. More details: Dynamics 365 Business Central: Using Security Filters and SecurityFiltering Property

So create a new field in Sales & Receivables Setup to save the excluded filter conditions.

Select the records to be excluded and click Hide selected to automatically add the filter to the new field and update the page. (Can be clicked multiple times in cumulative form)

Use Record.FilterGroup Method when filtering so that users cannot remove it. More details: Dynamics 365 Business Central: How to make some filters mandatory on the list page (users can not change or remove)

Test video:

Give it a try!!!😁

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50202 PostedSalesInvoicesExt extends "Posted Sales Invoices"
{
    actions
    {
        addbefore("Update Document")
        {
            action(Hideselected)
            {
                ApplicationArea = All;
                Caption = 'Hide selected';
                Image = Filter;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;

                trigger OnAction()
                var
                    SalesInvoiceHeader: Record "Sales Invoice Header";
                begin
                    Filter := '';
                    Rec.Reset();
                    SalesInvoiceHeader.Reset();
                    CurrPage.SetSelectionFilter(SalesInvoiceHeader);
                    SalesReceivablesSetup.Get();
                    Filter := SalesReceivablesSetup."Hidden Posted Sales Invoices";
                    SetExclusionFilter(SalesInvoiceHeader);
                    Rec.filtergroup(100);
                    Rec.SetFilter("No.", SalesReceivablesSetup."Hidden Posted Sales Invoices");
                    Rec.FilterGroup(0);
                end;
            }
        }
    }

    trigger OnOpenPage()
    begin
        SalesReceivablesSetup.Get();
        if SalesReceivablesSetup."Hidden Posted Sales Invoices" <> '' then begin
            Rec.filtergroup(100);
            Rec.SetFilter("No.", SalesReceivablesSetup."Hidden Posted Sales Invoices");
            Rec.FilterGroup(0);
        end;
    end;

    var
        SalesReceivablesSetup: Record "Sales & Receivables Setup";
        Filter: Text;

    local procedure SetExclusionFilter(var SalesInvoiceHeader: Record "Sales Invoice Header")
    begin
        if SalesInvoiceHeader.FindSet() then
            repeat
                if Filter = '' then
                    Filter := '<>' + Format(SalesInvoiceHeader."No.")
                else
                    Filter := Filter + '&' + '<>' + Format(SalesInvoiceHeader."No.");
            until SalesInvoiceHeader.Next() = 0;
        if Filter <> '' then begin
            SalesReceivablesSetup.Get();
            SalesReceivablesSetup."Hidden Posted Sales Invoices" := Filter;
            SalesReceivablesSetup.Modify(true);
        end;
    end;
}

tableextension 50200 SalesReceivablesSetupExt extends "Sales & Receivables Setup"
{
    fields
    {
        field(50200; "Hidden Posted Sales Invoices"; Text[2048])
        {
            Caption = 'Hidden Posted Sales Invoices';
            DataClassification = CustomerContent;
        }
    }
}

pageextension 50200 SalesReceivablesSetupPageExt extends "Sales & Receivables Setup"
{
    layout
    {
        addafter("Credit Warnings")
        {
            field("Hidden Posted Sales Invoices"; Rec."Hidden Posted Sales Invoices")
            {
                ApplicationArea = All;
            }
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL