When working with Microsoft Dynamics 365 Business Central, one of the most powerful capabilities is the dynamic handling of data using RecordRef. However, as a developer, you may have run into scenarios where you need to switch between the strongly typed Record
and the flexible RecordRef
—and wondered if there’s a clean way to do it.
Good news: Business Central now supports implicit conversion between Record
and RecordRef
Understanding the Core Types:
Before exploring the implicit conversion, it’s crucial to understand the distinct roles of Record
and RecordRef
:
Record
: A strongly-typed variable that represents a specific table in the Business Central database. The compiler enforces type safety, ensuring that operations performed on a Record
variable are valid for the defined table structure.RecordRef
: A more dynamic variable that provides a generic reference to any table in the Business Central database. It allows for runtime manipulation of table structures and data, often used in scenarios like generic data access, metadata exploration, and dynamic query building.With recent updates, AL now supports implicit conversion:
Record
to a RecordRef
RecordRef
back to a Record
(if the types match)This means cleaner, safer, and more readable code without the boilerplate RecordRef.GetTable()
or RecordRef.SetTable()
.
Example: Record to RecordRef
procedure LogAnyRecord(rec: RecordRef)
begin
Message('Table ID: %1', rec.Number);
end;
procedure RunLogging()
var
customer: Record Customer;
begin
customer.Get('10000');
LogAnyRecord(customer); // Implicit conversion from Record to RecordRef
end;
No need for recRef.SetTable(customer)
— it’s handled under the hood.
Example: RecordRef to Record
procedure GetCustomerName(recRef: RecordRef): Text
var
customer: Record Customer;
begin
customer := recRef; // Implicit conversion from RecordRef to Record
exit(customer.Name);
end;
Implicit conversions between Record
and RecordRef
bring AL language making it easier to write dynamic yet type-safe code. While it’s a small feature on the surface, it greatly enhances developer productivity and code clarity.
Stay Tuned for more updates.
Original Post https://ammolhsaallvi.blog/2025/04/09/implicit-record-and-record-ref-conversion-in-business-central-al/