Blog Post: Automatically Remove Unused Variables from Business Central Report Datasets with AL Navigator

Blog Post: Automatically Remove Unused Variables from Business Central Report Datasets with AL Navigator

As Business Central developers, we all know that report datasets can get messy over time. Variables are added, requirements change, and soon you’re left with unused columns cluttering your .al and .rdlc files. While these unused variables aren’t harmful, they make your code harder to read and maintain.

To address this, I’ve added a simple but practical feature to AL Navigator: the ability to automatically remove unused variables from report datasets.


Why Does This Matter?

Unused variables don’t cause errors, but they do create unnecessary noise. Over time, they make it harder to understand your code and reports. By keeping only the variables you need, you make your datasets cleaner, your reports easier to maintain, and your life as a developer a bit simpler.

This new feature is specifically designed to help report developers streamline their AL and RDLC files with minimal effort.


How It Works

Here’s how the functionality operates:

  1. Analyze the AL File: The tool scans the dataset definitions in your .al file, identifying variables defined for the report.
  2. Cross-Reference with the RDLC Layout: It parses the RDLC file to determine which variables are actually used in the layout.
  3. Remove the Unused Variables: Variables in the dataset that are not referenced in the RDLC are removed from the .al file. Similarly, unused fields are cleaned from the RDLC layout.

The functionality also provides feedback, listing the variables it removed. This ensures transparency and allows you to verify the cleanup.


Demo: Removing Unused Variables in Action

Let’s walk through a quick example.

Before Cleanup

Your .al file might look something like this:

 report 50100 "Sales Report"
{
    Caption = 'Sales Report';
    UsageCategory = Administration;
    ApplicationArea = All;
    RDLCLayout = 'SalesReport.rdl';

    dataset
    {
        dataitem(Customer; Customer)
        {
            column(UNUSED_BalanceLCY_Customer; "Balance (LCY)")
            {
            }
            column(SalesLCY_Customer; "Sales (LCY)")
            {
            }
            column(ProfitLCY_Customer; "Profit (LCY)")
            {
            }
        }
    }
} 

And your RDLC layout references only SalesLCY_Customer and ProfitLCY_CustomerUNUSED__BalanceLCY_Customer is defined in the dataset but isn’t used anywhere.

Running the Cleanup

With the AL file open in Visual Studio Code, trigger the functionality via the command palette (Ctrl+Shift+P)/F1 and select “Remove Unused Variables from Report Dataset”.

The tool:

  • Analyzes the dataset and layout.
  • Removes OldVariable from both the .al file and the RDLC layout.
  • Provides feedback like this:
plaintextCopy codeUpdated AL file: SalesReport.al
Removed variables:
- UNUSED_BalanceLCY_Customer
Updated RDL file: SalesReport.rdl

After Cleanup

Your .al file is now cleaner:

report 50100 "Sales Report"
{
    Caption = 'Sales Report';
    UsageCategory = Administration;
    ApplicationArea = All;
    RDLCLayout = 'SalesReport.rdl';

    dataset
    {
        dataitem(Customer; Customer)
        {
            
            column(SalesLCY_Customer; "Sales (LCY)")
            {
            }
            column(ProfitLCY_Customer; "Profit (LCY)")
            {
            }
        }
    }
} 

The RDL(C) file is also updated, leaving only the fields referenced in the layout.


A Simple but Useful Feature

While not groundbreaking, this small addition can save time and reduce clutter for report developers. By automating the cleanup process, you can focus on more important tasks while keeping your codebase tidy.

If you’re working on reports regularly, this feature is worth a try. It’s available now in the latest version of AL Navigator.


Try It Out

To use the feature, update your AL Navigator extension in Visual Studio Code and look for “Remove Unused Variables from Report Dataset” in the command palette. Please feel free to share any feedback here in the comments or report any problems on github.

If you are a report developer, my 9 tips for developing reports might be a good read for you.

2 thoughts on “Blog Post: Automatically Remove Unused Variables from Business Central Report Datasets with AL Navigator

    Mentions

  • 💬 Waldemar Brakowski

Leave a Reply

Your email address will not be published. Required fields are marked *