Dynamics 365 Business Central: How to make some filters mandatory on the list page (users can not change or remove)

Dynamics 365 Business Central

Hi, Readers.
Today I want to share a mini tip in Business Central. How to make some filters mandatory on the list page.
For example, when a user goes into the Sales Order List, the system applies a filter but the user can not remove it. And the user is not even aware of this filter.

Why is this needed? Years ago I had a client with very strict permission control, where salespeople could only view orders they had created. Only sales managers could view all of them.

So, you must automatically add the filter of ‘Creator’ while opening the list page, and the filter cannot be removed.

How to do it?
This time we will use Record.FilterGroup Method.

Record.FilterGroup Method: Gets or sets the filter group that is applied to a table.
Dynamics 365 Business Central uses the following filter groups internally. (-1..7)

NumberNameDescription
-1Cross-columnUsed to support the cross-column search.
0StdThe default group where filters are placed when no other group has been selected explicitly. This group is used for filters that can be set from the filter dialogs by the end user.
1GlobalUsed for filters that apply globally to the entire application.
2FormUsed for the filtering actions that result from the following:
– SetTableView Method (XMLport)SetTableView Method (Page)
– SourceTableView Property
– DataItemTableView Property.
3ExecUsed for the filtering actions that result from the following:
– SubPageView Property
– RunPageView Property
4LinkUsed for the filtering actions that result from the following:
– DataItemLink Property (Reports)
– SubPageLink Property
5TempNot currently used.
6SecurityUsed for applying security filters for user permissions.
7FactboxesUsed for clearing the state of factboxes.

We can achieve the above requirement by using the following features.

A filter set in a group different from filter group 0 cannot be changed by a user that uses a filter dialog to set a filter. If, for example, a filter has been set on customer numbers 1000 to 2000 in group 4, then the user can set a filter that delimits this selection further, but cannot widen it to include customer numbers outside the range 1000 to 2000.

So, In order not to affect standard functions, we can set a group greater than 7.

For the customer’s needs mentioned at the beginning:
1. Show User Name on Sales Order List page.

2. Add a new setup field on the User Setup page.

3. Add the FilterGroup Logic to the OnOpenPage trigger on the Sales Order List page. (For example: Group 100)

Test:
If the Allow viewing all orders is selected.

User can view all sales orders.

If the Allow viewing all orders is not selected. (Or is not added in User Setup)

Users can only view the sales orders they have created. And the filter can not be changed or removed.

Test Video:

Source Code:
User Setup Extension:

tableextension 50113 UserSetupExt extends "User Setup"
{
    fields
    {
        field(50100; "Allow viewing all orders"; Boolean)
        {
            Caption = 'Allow viewing all orders';
            DataClassification = CustomerContent;
        }
    }
}

pageextension 50113 UserSetupPageExt extends "User Setup"
{
    layout
    {
        addafter("Allow Posting To")
        {
            field("Allow viewing all orders"; Rec."Allow viewing all orders")
            {
                ApplicationArea = All;
            }
        }
    }
}

Sales Order List Extension:

tableextension 50112 SalesHeaderExt extends "Sales Header"
{
    fields
    {
        field(50100; UserName; Code[50])
        {
            Caption = 'User Name';
            Editable = false;
            FieldClass = FlowField;
            CalcFormula = lookup(User."User Name" where("User Security ID" = field(SystemCreatedBy)));
        }
    }
}

pageextension 50112 SalesOrderExt extends "Sales Order List"
{
    layout
    {
        addafter("Sell-to Customer Name")
        {
            field(SystemCreatedBy; Rec.SystemCreatedBy)
            {
                ApplicationArea = All;
            }
            field(UserName; Rec.UserName)
            {
                ApplicationArea = All;

            }
        }
    }

    trigger OnOpenPage()
    var
        UserSetup: Record "User Setup";
    begin
        if UserSetup.Get(UserId) then
            if UserSetup."Allow viewing all orders" then
                exit;
        Rec.FilterGroup(100);
        Rec.SetRange(UserName, UserId);
        Rec.FilterGroup(0);
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL