With recent updates to the AL language extension (v15), Microsoft has introduced support for implicit conversion between Record and RecordRef instances, making it easier to work with both types in the code.
This feature removes some of the verbosity previously required when switching between Record and RecordRef variables, allowing developers to write cleaner, more concise AL code.
Implicit Conversion from Record to RecordRef
Current Approach:
RecRef.GetTable(Customer)
New Approach:
RecRef := Customer;
Implicit Conversion from RecordRef to Record
Current Approach:
RecRef.SetTable(Customer)
New Approach:
Customer := RecRef;
Important: It will throw a runtime error if RecordRef does not reference the same table as the Record variable. Always ensure the types align!
Also, passing a RecordRef to a function expecting a specific Record works!
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)
begin
case rr.Number of
Database::Customer:
Message('Process Customer');
else
Error('Unable to process record');
end;
end;
}
Regards,
Tharanga Chandrasekara