Business Central 2025 wave 2 (BC27): RecordRef – Get/Search field by name (New overloads)

Dynamics 365 Business Central

Hi, Readers.
The public preview for Dynamics 365 Business Central 2025 release wave 2 (BC27) is available. Learn more: Link.

I will continue to test and share some new features that I hope will be helpful. In this post, I would like to talk about RecordRef – Get field by name (New overloads).

This new feature is not yet documented in the Business Central 2025 release wave 2 (BC27) release plan but is mentioned in AL Language extension changelog Version 16.0

New overloads are added to the Field and FieldExist methods, which allows searching for a field by its name.

Changelog | Visual Studio Marketplace

As you might know, we can use RecordRef Data Type and FieldRef Data Type to simply get Field Name or Field Caption from Field Id.
For example: FieldRef.Name() or FieldRef.Caption()

In How to get the id, type, length and value of a field by its text name (field name), we briefly discussed how to get the id, type, length and value of a field by its text name (field name).

With this wave (BC27), Microsoft has brought us an easier way. New overloads are added to the Field and FieldExist methods, which allows searching for a field by its name.
First let’s look at the two standard methods.

RecordRef.Field(Integer) Method: Gets a FieldRef for the field that has the number FieldNo in the table that is currently selected. If no field has this number, the method returns an error.

RecordRef.FieldExist(Integer) Method: Determines if the field that has the number FieldNo exists in the table that is referred to by the RecordRef. Returns an error if no table is currently selected.

Overloads added this time: (It seems that the description of the procedure has not been updated)

procedure Field(FieldName: Text): FieldRef
Gets a FieldRef for the field that has the number FieldNo in the table that is currently selected. If no field has this number, the method returns an error.

procedure FieldExist(FieldName: Text): Boolean
Determines if the field that has the number FieldNo exists in the table that is referred to by the RecordRef. Returns an error if no table is currently selected.

Here is a simple example:

Test code:

pageextension 50100 MyExtension extends "Customer List"
{
    trigger OnOpenPage()
    var
        MyRecordRef: RecordRef;
        MyFieldRef: FieldRef;
        Text000: Label 'The table contains %1 field, field id is %2.';
    begin
        MyRecordRef.Open(Database::Customer);
        if MyRecordRef.FieldExist('Name') then begin
            MyFieldRef := MyRecordRef.Field('Name');
            Message(Text000, MyFieldRef.Name, MyFieldRef.Number);
        end;
    end;
}

Great, give it a try!!!😁

PS:

‘Field’ is not available in runtime version ‘15.0’. The supported runtime versions are: ‘16.0’ or greater. AL AL0666

‘FieldExist’ is not available in runtime version ‘15.0’. The supported runtime versions are: ‘16.0’ or greater. AL AL0666

END

Hope this will help.

Thanks for reading.

ZHU

コメント