Dynamics 365 Business Central: How to create a universal record selection/lookup page – arbitrary table

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly talk about an interesting topic, how to create a universal record selection/lookup page – for arbitrary table in Business Central.
We’ve discussed how to create a Lookup, Drop-Down, or Option list (Single and Multi select) before, this is not very difficult. But this time I’m going to share a more interesting way, using codeunit 9555 “Record Selection”.

codeunit 9555 “Record Selection”: Exposes functionality to look up records.
procedure Open: Opens the record lookup page and assigns the selected records on the <paramref name=”SelectedRecord”/> parameter.

codeunit 9556 “Record Selection Impl.”:

First let’s look at a simple example.

So using this method, we only need to know the Table id, the Maximum Count of the table, and a variable “Record Selection Buffer” to open a lookup window and get the value inside.
page 9555 “Record Lookup”: The lookup window

table 9555 “Record Selection Buffer”:

This universal lookup window supports up to 10 fields. Next, let me create a more general function. Users can select the Table ID first, then click on the Assist Edit icon to open the lookup window and fill in the selected record information into the fields below in order.

If the opened table does not contain any records, the following error will occur.

If there is only one record in the table, the lookup window will not open and it will be selected automatically.

Test video:

At this point perhaps you still have a question, what determines the fields of the opened lookup window? This shows the fields in the fieldgroups. More details: Field groups (DropDown controls)

If the Brick fieldgroup is set, the fields in the Brick fieldgroup are displayed.
table 18 Customer:

table 27 Item:

If the Brick fieldgroup is not set, but the DropDown fieldgroup is set, the fields in the DropDown fieldgroup are displayed.
table 14 Location:

If neither is set, the primary key will be displayed.
table 2000000006 Company:

Great, 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)

tableextension 50118 CustomerExt extends Customer
{
    fields
    {
        field(50100; "Table ID"; Integer)
        {
            TableRelation = AllObjWithCaption."Object ID" where("Object Type" = CONST(Table));
            DataClassification = SystemMetadata;
        }
        field(50102; "Field 1"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50103; "Field 2"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50104; "Field 3"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50105; "Field 4"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50106; "Field 5"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50107; "Field 6"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50108; "Field 7"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50109; "Field 8"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50110; "Field 9"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
        field(50111; "Field 10"; Text[250])
        {
            DataClassification = SystemMetadata;
        }
    }
}

pageextension 50119 CustomerCardExt extends "Customer Card"
{
    layout
    {
        addafter(General)
        {
            group(Lookup)
            {
                field("Table ID"; Rec."Table ID")
                {
                    ApplicationArea = All;
                    trigger OnValidate()
                    begin
                        Rec."Field 1" := '';
                        Rec."Field 2" := '';
                        Rec."Field 3" := '';
                        Rec."Field 4" := '';
                        Rec."Field 5" := '';
                        Rec."Field 6" := '';
                        Rec."Field 7" := '';
                        Rec."Field 8" := '';
                        Rec."Field 9" := '';
                        Rec."Field 10" := '';
                    end;

                    trigger OnAssistEdit()
                    var
                        RecordSelection: Codeunit "Record Selection";
                        RecordSelectionBuffer: Record "Record Selection Buffer";
                        RecRef: RecordRef;
                        MaximumCount: Integer;
                    begin
                        RecordSelectionBuffer.Reset();
                        RecordSelectionBuffer.DeleteAll();
                        MaximumCount := 0;

                        RecRef.Open(Rec."Table ID");
                        MaximumCount := RecRef.Count;
                        if RecordSelection.Open(Rec."Table ID", MaximumCount, RecordSelectionBuffer) then begin
                            Rec."Field 1" := RecordSelectionBuffer."Field 1";
                            Rec."Field 2" := RecordSelectionBuffer."Field 2";
                            Rec."Field 3" := RecordSelectionBuffer."Field 3";
                            Rec."Field 4" := RecordSelectionBuffer."Field 4";
                            Rec."Field 5" := RecordSelectionBuffer."Field 5";
                            Rec."Field 6" := RecordSelectionBuffer."Field 6";
                            Rec."Field 7" := RecordSelectionBuffer."Field 7";
                            Rec."Field 8" := RecordSelectionBuffer."Field 8";
                            Rec."Field 9" := RecordSelectionBuffer."Field 9";
                            Rec."Field 10" := RecordSelectionBuffer."Field 10";
                            Rec.Modify(true);
                        end;
                    end;
                }
                field("Field 1"; Rec."Field 1")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 2"; Rec."Field 2")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 3"; Rec."Field 3")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 4"; Rec."Field 4")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 5"; Rec."Field 5")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 6"; Rec."Field 6")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 7"; Rec."Field 7")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 8"; Rec."Field 8")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 9"; Rec."Field 9")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
                field("Field 10"; Rec."Field 10")
                {
                    ApplicationArea = All;
                    Editable = false;
                }
            }
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL