web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Query based Report Filtering in AL Language?

(5) ShareShare
ReportReport
Posted on by 17
I designed in AL a report based on a query dataitem. How do I add filters to this? I cannot even add filters at runtime when running the report in the auto requestpage. What gives?
 
I have the same question (0)
  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,284 Super User 2025 Season 2 on at
    You should be able to use filter on your query.
     
    Check this two blog posts for reference.
     
  • Suggested answer
    YUN ZHU Profile Picture
    95,331 Super User 2025 Season 2 on at
  • Suggested answer
    Nitin Verma Profile Picture
    21,698 Moderator on at
    Hi,
    you can try something like this:
     
    report 50100 "MyQueryReport"
    {
        UsageCategory = ReportsAndAnalysis;
        dataset
        {
            dataitem(MyQuery; "MyQuery")
            {
                column(No; No) { }
                column(Name; Name) { }
                column(BalanceLCY; BalanceLCY) { }
            }
        }
        requestpage
        {
            layout
            {
                area(content)
                {
                    group(Filters)
                    {
                        field(MinBalance; MinBalanceFilter)
                        {
                            ApplicationArea = All;
                            Caption = 'Minimum Balance';
                            trigger OnValidate()
                            begin
                                // Validate and store the filter value
                            end;
                        }
                    }
                }
            }
        }
        var
            MinBalanceFilter: Decimal;
    
        trigger OnPreReport()
        begin
            // Apply the filter from the request page to the query
            if MinBalanceFilter <> 0 then
                MyQuery.SetFilter(BalanceLCY, '>=%1', MinBalanceFilter);
        end;
    }
     
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,275 Super User 2025 Season 2 on at
  • CU05031656-0 Profile Picture
    17 on at
    I am not looking to filter the query, I am looking for filtering the report at runtime.
     
    This is where I do not have any filtering. If I click on +Filter I get this, regardless, of what I have.
     
    No actual fields I need to filter on.
     
    Sure, I could make a personalized RequestPage, but there are two issues with this:
     
    1. I will miss the drop-down pick of actual values for the filtering fields, like below.
     
     
    and 2.
     
    In the requestpage definition in the report object, again there is no filters I could actually use.
     
  • CU05031656-0 Profile Picture
    17 on at
    @Nitin Verma
     
    Not sure where this syntax comes from as AL definitely expects a table inthere not a query.
     
     
    I cannot make a query work there, it only accepts a table.
  • Gerardo Rentería García Profile Picture
    25,169 Most Valuable Professional on at

    Hi, good day
    I hope this can help you, and give you some hints.

    How Do I: Create a Report based upon a Query? – think about IT

    Best Regards
    Gerardo

  • Suggested answer
    Nitin Verma Profile Picture
    21,698 Moderator on at
    Hi,
    I have created this whole report now, can you try now, replace your actual values here:
    report 50100 "MyQueryReport"
    {
        UsageCategory = ReportsAndAnalysis;
        DefaultLayout = RDLC;
        RDLCLayout = './MyQueryReport.rdlc'; // Optional: Add your layout file
    
        dataset
        {
            dataitem(Integer; Integer)
            {
                DataItemTableView = where(Number = const(1..));
                column(No; CustNo)
                {
                    Caption = 'Customer No.';
                }
                column(Name; CustName)
                {
                    Caption = 'Customer Name';
                }
                column(BalanceLCY; CustBalanceLCY)
                {
                    Caption = 'Balance (LCY)';
                }
    
                trigger OnPreDataItem()
                begin
                    // Apply filters to the query before opening it
                    if MinBalanceFilter <> 0 then
                        MyQuery.SetFilter(BalanceLCY, '>=%1', MinBalanceFilter);
    
                    MyQuery.Open(); // Open the query with any applied filters
                end;
    
                trigger OnAfterGetRecord()
                begin
                    // Read the next row from the query
                    if not MyQuery.Read() then
                        CurrReport.Break(); // Exit when no more rows
    
                    // Assign query values to report variables
                    CustNo := MyQuery.No;
                    CustName := MyQuery.Name;
                    CustBalanceLCY := MyQuery.BalanceLCY;
                end;
            }
        }
    
        requestpage
        {
            layout
            {
                area(content)
                {
                    group(Filters)
                    {
                        field(MinBalance; MinBalanceFilter)
                        {
                            ApplicationArea = All;
                            Caption = 'Minimum Balance';
                            trigger OnValidate()
                            begin
                                // Validate the input if needed
                                if MinBalanceFilter < 0 then
                                    Error('Minimum Balance cannot be negative.');
                            end;
                        }
                    }
                }
            }
        }
    
        var
            MyQuery: Query "MyQuery"; // Reference to your query object
            CustNo: Code[20];         // Variables to hold query column values
            CustName: Text[100];
            CustBalanceLCY: Decimal;
            MinBalanceFilter: Decimal; // Variable for runtime filter from request page
    }
    
    query 50100 "MyQuery"
    {
        QueryType = Normal;
        elements
        {
            dataitem(Customer; Customer)
            {
                column(No; "No.") { }
                column(Name; Name) { }
                column(BalanceLCY; "Balance (LCY)") { }
            }
        }
    }
     
  • Suggested answer
    Ramiz Profile Picture
    597 on at
    Hi,

    You should have the table dataitem in the report in order to use the default filtering like you mentioned.

    If you add the table (Sales Invoice Header) as dataitem you will be able to apply the filter.
  • CU05031656-0 Profile Picture
    17 on at
    @Nitin Verma
     
     
    These names are NOT known in the context.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,143

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,694 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans