Dynamics 365 Business Central: How to use Page.SetSelectionFilter Method in temporary records

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share another mini tip about Business Central, how to use Page.SetSelectionFilter Method in temporary records.

Page.SetSelectionFilter(var Record) is a very commonly used method, which can get the value currently selected by the user.

Page.SetSelectionFilter(var Record): Notes the records that the user has selected on the page, marks those records in the table specified, and sets the filter to “marked only”.

Here is a simple example.

Very simple.

PS: Dynamics 365 Business Central: About the automatic filtering problem after using Page.SetSelectionFilter Method

There is a small problem here. If the variable is temporary, the value will not be obtained. Because we did not assign values ​​to this temporary table in advance.
PS: Dynamics 365 Business Central: How to check if a record/table is temporary via AL

So when we want to pass the selected value to the temporary record, additional processing is required. There are two situations here.

1. On pages with a normal source table (Not temporary)

You need to get the data from the actual record and then insert the data into the temporary record.

Test code:

pageextension 50112 CustomerListExt extends "Customer List"
{
    actions
    {
        addfirst(processing)
        {
            action(Count)
            {
                ApplicationArea = All;
                Caption = 'Count selected lines';
                Promoted = true;
                PromotedCategory = Process;
                Image = CalculateBalanceAccount;
                trigger OnAction()
                var
                    Cust: Record Customer;
                    TempCust: Record Customer temporary;
                begin
                    TempCust.Reset();
                    TempCust.DeleteAll();
                    Cust.Reset();
                    CurrPage.SetSelectionFilter(Cust);
                    if Cust.FindSet() then
                        repeat
                            TempCust.Init();
                            TempCust.TransferFields(Cust);
                            TempCust.Insert();
                        until Cust.Next() = 0;
                    Message('Count: %1', TempCust.Count());
                end;
            }
        }
    }
}

2. On pages with a temporary source table

For retrieving selected records on pages with a temporary source table, the following example shows how to define a custom method on a page that is based on a temporary Contact table. This is an example from Microsoft,
Example 2 (temporary records).

The key point is TempContact.Copy(Rec, true);

Record.Copy(Record [, Boolean]) Method: Copies a specified record’s filters, views, automatically calculated FlowFields, marks, fields, and keys that are associated with the record from a table or creates a reference to a record.

[Optional] ShareTable
 Type: Boolean
Specifies whether the method copies filters, views, automatically calculated FlowFields, marks, fields, and keys of the record or creates a reference to a temporary record. If FromRecord and Record are both temporary and ShareTable is true, then the COPY method causes Record to reference the same table as FromRecord. If ShareTable is true, then both Record and FromRecord must be temporary; otherwise an error will occur. The default value is false. If you specify false, only filters, marks, and keys are copied.

Note that if the original table is not a temporary table, [Optional] ShareTable = true cannot be used.

The COPY function can only be used with the shareTable argument set to true if both records are temporary.

Of course, we can also use it directly without declaring the new method. For example,

Test code:

page 50112 "New Customer List"
{
    PageType = List;
    Caption = 'New Customer List';
    ApplicationArea = All;
    UsageCategory = Lists;
    SourceTable = Customer;
    SourceTableTemporary = true;

    layout
    {
        area(content)
        {
            repeater(Control1)
            {
                field("No."; Rec."No.")
                {
                    ApplicationArea = All;
                }
                field("Name"; Rec.Name)
                {
                    ApplicationArea = All;
                }
                field(Contact; Rec.Contact)
                {
                    ApplicationArea = All;
                }
                field(Balance; Rec.Balance)
                {
                    ApplicationArea = All;
                }
                field("Sales (LCY)"; Rec."Sales (LCY)")
                {
                    ApplicationArea = All;
                }
            }
        }
    }

    actions
    {
        area(Processing)
        {
            action(Count)
            {
                ApplicationArea = All;
                Caption = 'Count selected lines';
                Promoted = true;
                PromotedCategory = Process;
                Image = CalculateBalanceAccount;
                trigger OnAction()
                var
                    TempCust: Record Customer temporary;
                begin
                    TempCust.Reset();
                    TempCust.DeleteAll();
                    TempCust.Copy(Rec, true);
                    CurrPage.SetSelectionFilter(TempCust);
                    Message('Count: %1', TempCust.Count());
                end;
            }
        }
    }

    trigger OnOpenPage()
    var
        Cust: Record Customer;
    begin
        Rec.Reset();
        Rec.DeleteAll();
        Cust.Reset();
        if Cust.FindSet() then
            repeat
                Rec.Init();
                Rec := Cust;
                Rec.Insert();
            until Cust.Next() = 0;
    end;
}

Very simple. Give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL