How to let users choose field(s) properly

by Feb 6, 2022AL Language

Home 9 Development 9 AL Language 9 How to let users choose field(s) properly

When some complex functionality is developed, it is sometimes necessary to let users choose a specific field. This can be required for field permissions, mapping imported values or any similar process.

Earlier, the usual way was to create a link on the Field table (for example “Field”.”No.” WHERE (TableNo=FIELD(Table ID))). As you can see, you have to first specify (let the user choose) the table. That means that the user has to use two lookup pages. Furthermore, if you want to let users choose more than one field, the script had to be programmed much more complicated way.

However, with all the new changes to best practices that Microsoft has done during the last two years, there is a new way how to let users choose fields properly. All needed is included in codeunit 9806 “Field Selection” (codeunit 9807 “Field Selection Impl.”).

We only need to call method Open(var Field: Record “Field”): Boolean and the system will do everything automatically. The method returns true when a field is selected, false otherwise. The selected field is returned as a selected record of the Field record parameter.

local procedure LetUserChoose()
var
    Field: Record Field;
    FieldSelection: Codeunit "Field Selection";

    SelectedFieldMsg: Label 'Field %1 in table %2 of type %3 has been selected.', Comment = '%1 - Caption of the field, %2 - Caption of the table, %3 - data type of the field';
    NoFieldSelectedMsg: Label 'No field has been selected.';
begin
    if FieldSelection.Open(Field) then
        Message(SelectedFieldMsg, Field."Field Caption", Field.TableCaption, Field.Type);
    Message(NoFieldSelectedMsg);
end;

If we look at the implementation in more detail (codeunit 9807 “Field Selection Impl.”) we can see that some of the fields are automatically excluded from selection – already removed fields (Removed-Obsoleted) and fields that are disabled.

local procedure HideInvalidFields(var "Field": Record "Field")
begin
    Field.FilterGroup(2);
    Field.SetFilter(ObsoleteState, '<>%1', Field.ObsoleteState::Removed);
    Field.SetRange(Enabled, true);
    Field.FilterGroup(0);
end;

Another interesting thing about this functionality is how the selected field (or fields!). All of this depends on the Field record parameter provided in FieldSelection.Open(var Field: Record “Field”): Boolean method. It works differently with Temporary and Non-Temporary records.

If you provide a Non-Temporary Field record, the selected field(s) is returned filtered using the SetSelectionFilter system method. On the other hand, if you provide a Temporary Field record, returned variable contains unfiltered results that contain only selected field(s).

Of course, you can filter fields that will be shown to the user – by applying filters on the Non-Temporary Field record or by inserting only specific fields into the Temporary Field record.

procedure GetSelectedFields(var SelectedField: Record "Field")
var
    "Field": Record "Field";
begin
    if SelectedField.IsTemporary() then begin
        SelectedField.Reset();
        SelectedField.DeleteAll();
        CurrPage.SetSelectionFilter(Field);
        if Field.FindSet() then
            repeat
                SelectedField.Copy(Field);
                SelectedField.Insert();
            until Field.Next() = 0;
    end else begin
        CurrPage.SetSelectionFilter(SelectedField);
        SelectedField.FindSet();
    end;
end;

Side note: the name of the field’s table is shown only when the fields are from more than one table. Otherwise, the table name column is not shown.

Recent Articles from the category

Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use? This is the second part of my new article series about Collectible Errors. Let's check out the first part here: Collectible Errors?! | Microsoft Dynamics 365 - Ing. Tomáš Kapitán (kepty.cz) or you might be interested in my...

read more
Collectible Errors?!

Collectible Errors?!

Collectible Errors?! It has been already almost a year since ErrorInfo datatype & CollectibleErrors were introduced (I already have an article about basic structure: ErrorInfo data type & Collectible Errors). This article was released for the first time in...

read more
Substituting standard reports

Substituting standard reports

Report objects cannot be extended in versions previous to the Business Central 2021 release wave 1 version when ReportExtensions object was introduced. Since then, many of changes we need to do in reports can be done without creating a new copy of object. On the other...

read more
Isolated events

Isolated events

With Business Central 2022 wave 1, a new setting for event publishers was introduced. Until this version, any error in any event subscriber caused interruption to the current running process and stopped the whole activity. In some cases (such as log-in), this is...

read more
Custom Filter Tokens

Custom Filter Tokens

As a user of the Business Central you have some constants you can use to filter or insert values. These constants contain useful values for data manipulation such as t / today for date field - return current dateq / quarter for date field filters - return range of the...

read more
Working with Translation Files (XLIFF)

Working with Translation Files (XLIFF)

How does the translation file work? In Microsoft Dynamic 365 Business Central, displaying different languages is done using the XLIFF files extension (.xlf). The generated XLIFF files have strings that are specified in properties like Captions, Tooltips......

read more
1D & 2D Barcodes in Business Central 19.1

1D & 2D Barcodes in Business Central 19.1

Business Central (cloud-only!) includes one-dimensional (1D) barcode fonts since Microsoft Dynamics 365 Business Central 2020 wave 2 (17.0) was released in October 2020. It is a great improvement to how developers can handle requirements from their clients to print...

read more
ErrorInfo data type & Collectible Errors

ErrorInfo data type & Collectible Errors

A major change to AL Language development is here! Collectible errors can hugely improve any validation procedures. And what's new? Everything is about a new data type ErrorInfo. ErrorInfo data type ErrorInfo is a new data type introduced in Microsoft Dynamics 365...

read more
Exploring CosmosDB with Business Central, part 2

Exploring CosmosDB with Business Central, part 2

This part explains how to connect to Azure Cosmos DB from your Business Central extension using AL Language and how to get some values from the database.------------------------------------------------------------------------------------------------------------------...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here