Data Access Strategies in AL - Base Application and Extension Modules

  2 minute read

When developing in AL for Microsoft Dynamics 365 Business Central, accessing data from tables—whether part of the base application or provided by ISVs and Microsoft extensions—can be straightforward or challenging, depending on how the table is exposed.

Accessing Base Application Tables

For tables included in the base application, such as the Customer table, you simply declare the table in your AL code and access its data directly. This process is well-supported by AL’s symbols and IntelliSense, making development efficient and readable.

Example: Accessing the Customer Table

var
    CustomerRec: Record Customer;
begin
    if CustomerRec.FindFirst() then
        Message('Customer Name: %1', CustomerRec.Name);
end;

This code declares a record variable for the Customer table and displays the name of the first customer.

Working with Extension Tables

When you need to access tables from other extensions (ISV or Microsoft), things get more complex. These tables are not accessible by default unless you download the necessary symbols. To do this:

  • Identify the Extension: Use the Page Inspection tool in Business Central. The Extensions tab will show you which extension a record belongs to.
  • Declare Dependencies: In your app.json, declare the extension as a dependency.
  • Download Symbols: Once dependencies are set, download the symbols to access the table and its fields.</li>
  • Customize as Needed: You can then create your own extension to interact with these objects or customize them for your requirements.

Example of Page Inspection

Example: Declaring a Dependency in app.json

"dependencies": [
    {
        "id": "b696b4c9-637c-49d1-a806-763ff8f0a20e",
        "name": "IRS Forms",
        "publisher": "Microsoft",
        "version": "27.0.38460.39615"
    }
]

This snippet shows how to declare a dependency on an ISV extension in your app.json file.

Alternative: Using RecordRef and FieldRef

If you need to access data from an extension without downloading symbols, you can use AL’s RecordRef and FieldRef objects. This method allows you to work with tables and fields by their IDs, bypassing the need for direct symbol access.

var
    RecRef: RecordRef;
    FieldRef: FieldRef;
    FieldValue: Variant;
begin
    RecRef.Open(10030); // Opens the table with ID 10030
    FieldRef := RecRef.Field(1); // Accesses the field with ID 1
    FieldValue := FieldRef.Value;
    Message('Field Value: %1', FieldValue);
end;

This code opens a table by its ID and retrieves the value of a field by its ID.

  • No IntelliSense: You won’t get field suggestions, making code harder to read and maintain.
  • Field IDs Required: You must know the exact IDs for tables and fields.
  • Best for Limited Access: This approach is suitable for reading a small number of fields or setup values, but not recommended for heavy customizations or complex logic.

Best Practices and Recommendations

  • Use Page Inspection: Always start by identifying the extension a record belongs to.
  • Declare Dependencies for Customizations: For significant customizations, declare dependencies and download symbols for full access and maintainability.
  • Use RecordRef Sparingly: Reserve RecordRef/FieldRef for lightweight scenarios where adding a dependency is unnecessary.

Conclusion

Navigating table access in AL requires understanding both the technical and practical aspects of Business Central’s extensibility model. By leveraging the right tools and approaches, you can ensure your customizations are robust, maintainable, and aligned with best practices.

Leave a comment