Business Central 2025 wave 1 (BC26.2): Implicit conversion between Record and RecordRef

Dynamics 365 Business Central

Hi, Readers.
Dynamics 365 Business Central 2025 wave 1 (BC26) is generally available. More details: General Available: Dynamics 365 Business Central 2025 release wave 1 (BC26).
And minor update 26.2 for Business Central 2025 release wave 1 has been released for Business Central online. More details: Cumulative Update Summary for Microsoft Dynamics 365 Business Central (June, 2025)

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 implicit conversion between Record and RecordRef.

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

Implicit conversion between Record and RecordRef
We have introduced support for implicit conversion between Record and RecordRef instances, allowing direct assignment between these types.

codeunit 10 RecordAndRecordRefConversion
{

    procedure RecordToRecordRef()
    var
        Customer: Record Customer;
        RecRef: RecordRef;
    begin
        RecRef := Customer;      // Similar to RecRef.GetTable(Customer);
        ProcessRecord(Customer); // Argument conversion
    end;

    procedure RecordRefToRecord()
    var
        Customer: Record Customer;
        RecRef: RecordRef;
    begin
        Customer := RecRef;      // Similar to RecRef.SetTable(Customer); This will cause an error if the table is different
        ProcessCustomer(RecRef); // Argument conversion.
    end;

    procedure ProcessCustomer(r: record Customer)
    begin
        Message('Process Customer');
    end;

    procedure ProcessRecord(rr: RecordRef)
    var
        Customer: record Customer;
    begin
        case rr.Number of
            Database::Customer:
                Message('Process Customer');
            else
                Error('Unable to process record');
        end;
    end;
}
https://marketplace.visualstudio.com/items/ms-dynamics-smb.al/changelog

Let’s see more details.

Record -> RecordRef

The RecordRef Data type can refer to any table in the database, you can use the Open method to use the table number to select the table that you want to access, or use the GetTable method to use another record variable to select the table that you want to access.

For example,

Or

In runtime version 15.2 or above, we can do this directly through the assignment statement, RecordRef :=Record.

Test code:

pageextension 50122 CustomerListExt extends "Customer List"
{
    actions
    {
        addfirst(processing)
        {
            action(CountCustomers)
            {
                ApplicationArea = All;
                Caption = 'Count Customers';
                Image = GetSourceDoc;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    MyRecordRef: RecordRef;
                    Text000: Label 'The %1 table contains %2 records.';
                    Cust: Record Customer;
                begin
                    MyRecordRef := Cust;
                    Message(Text000, MyRecordRef.Caption, MyRecordRef.Count);
                    MyRecordRef.Close;
                end;
            }
        }
    }
}

RecordRef -> Record

Let’s look at another simple example. We can use RecordRef.SetTable(Record) Method to set the table to which a Record variable refers as the same table as a RecordRef variable.

Same as above, in runtime version 15.2 or above, we can do this directly through the assignment statement, Record := RecordRef.

Test code:

pageextension 50122 CustomerListExt extends "Customer List"
{
    actions
    {
        addfirst(processing)
        {
            action(CountCustomers)
            {
                ApplicationArea = All;
                Caption = 'Count Customers';
                Image = GetSourceDoc;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    MyRecordRef: RecordRef;
                    Text000: Label 'The  %1 table contains %2 records.';
                    Cust: Record Customer;
                begin
                    MyRecordRef.Open(Database::Customer);
                    //MyRecordRef.SetTable(Cust);
                    Cust := MyRecordRef;
                    Message(Text000, Cust.TableName, Cust.Count);
                    MyRecordRef.Close;
                end;
            }
        }
    }
}

PS: This does not replace RecordRef.SetTable(Record, Boolean) Method, for example, MyRecordRef.SetTable(TempCust, true);

The following method does not return any value. (It seems that the assignment statement does not copy filters and views)

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

Test code:

pageextension 50122 CustomerListExt extends "Customer List"
{
    actions
    {
        addfirst(processing)
        {
            action(CountCustomers)
            {
                ApplicationArea = All;
                Caption = 'Count Customers';
                Image = GetSourceDoc;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    MyRecordRef: RecordRef;
                    Text000: Label 'The temporary %1 table contains %2 records.';
                    Cust: Record Customer;
                    TempCust: Record Customer temporary;
                begin
                    MyRecordRef.Open(Database::Customer, true);
                    Cust.Reset();
                    Cust.SetRange("No.", '10000', '30000');
                    if Cust.FindSet() then
                        repeat
                            MyRecordRef.Init();
                            MyRecordRef.Field(1).Value := Cust."No.";
                            MyRecordRef.Field(2).Value := Cust.Name;
                            MyRecordRef.Insert();
                        until Cust.Next() = 0;
                    //MyRecordRef.SetTable(TempCust, true);
                    TempCust := MyRecordRef;
                    Message(Text000, TempCust.TableName, TempCust.Count);
                    MyRecordRef.Close;
                end;
            }
        }
    }
}

And please note that if the runtime is not set to 15.2 or above, the following error will be displayed.

Cannot implicitly convert type ‘Record Customer’ to ‘RecordRef” AL AL0122

Cannot implicitly convert type ‘RecordRef’ to ‘Record Customer’ AL AL0122

Great. Give it a try!!!😁

PS:
Business Central 2025 wave 1 (BC26): Added SetAutoCalcFields Method on the RecordRef data type

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL