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...
Answered

Dynamic date filtering in a AL query

(1) ShareShare
ReportReport
Posted on by 30
Hi there,
 
Hoping somebody can give me the hint to solve my small riddle. In essence I would like to pre-filter for overdue invoices with the help of a query (see below for AL code). That works fine if I am using a hardcoded solution by specifying a day for the 'ColumnFilter' within the dataitem. E.g. if today is the 20th of a month the code is returning all invoices summed up until the 19th of the current month. 
Now, obvious that's not a solution that works for every day of a month. I thought, I could achieve my goal by setting the filter via the trigger method, but the return results are identical with a non-filtered item.
 
Please tell me, what am I missing for a dynamic solution?
 
PS: this answer didn't worked for me
 
query 10001 SummedUpInvoices
{
    Caption = 'Open Invoices'; // without any date filter
    //Caption = 'Overdue Invoices'; // with date filter 'earlier than today' applied
    QueryType = Normal;
    QueryCategory = 'Sales Invoice List';

    elements
    {
        dataitem(SalesInvoiceHeader; "Sales Invoice Header")
        {
            column(JobNo; "Job No.")
            {
            }
            filter(DueDate; "Due Date")
            {
                // ColumnFilter = DueDate = filter(< 20); // hardcoded, non-dynamical solution
            }
            column(AmountIncludingVAT; "Amount Including VAT")
            {
                Method = Sum;
            }
        }
    }

    trigger OnBeforeOpen()
    begin
        TodayDate := Today();
        //CurrQuery.SetFilter(DueDate, '< %1', TodayDate); // dynamic approach, but doesn't work
    end;

    var
        TodayDate: Date;

}
 
  • Verified answer
    VeNeZ Profile Picture
    VeNeZ 30 on at
    Dynamic date filtering in a AL query
    If I am being honest, I do not know what now made the difference but it works with the code as per below for past respectively upcoming invoices. 
     
    Nevertheless, thanks for the inspiration and your time.
     
        trigger OnBeforeOpen()
        begin
            // For invoices in the past
            CurrQuery.SetFilter(DueDate, '<%1', Today);
            // or alternative via a range where the start is specified in the far past
            //CurrQuery.SetRange(DueDate, DMY2Date(1, 1, 2010), Today);
            //
            // 
            // For upcoming invoices
            //CurrQuery.SetFilter(DueDate, '>%1', Today);
            // or alternative via a range where the end is specified in the far future
            //CurrQuery.SetRange(DueDate, Today, DMY2Date(1, 1, 2100));
            // 
        end;
     
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 77,925 Super User 2025 Season 1 on at
    Dynamic date filtering in a AL query
    I tested it briefly and there is no problem.
     
     
    My test code:
    query 50200 "Customer_Sales_Quantity"
    {
        QueryType = Normal;
        QueryCategory = 'Customer List';
    
        elements
        {
            dataitem(C; Customer)
            {
                column(Customer_Number; "No.")
                {
                }
    
                column(Customer_Name; Name)
                {
                }
    
                dataitem(SL; "Sales Line")
                {
                    DataItemLink = "Sell-to Customer No." = c."No.";
                    SqlJoinType = InnerJoin;
    
                    column(Qty; Quantity)
                    {
                    }
                    column(Posting_Date; "Posting Date")
                    {
                    }
                }
            }
        }
        trigger OnBeforeOpen()
        begin
            currQuery.SetRange(Posting_Date, Today);
        end;
    }
     
  • VeNeZ Profile Picture
    VeNeZ 30 on at
    Dynamic date filtering in a AL query
    Thanks for your response, but as mentioned (and written directly in the code as well) the 'OnOpenBefore' trigger method doesn't work (for unknown reason to myself). Any idea, what is wrong there?
     
    On the other topic of 'object that runs the query': as I am using the 'QueryCategory' property, the option to execute is given me on the page(s) I specified there. Do you mean I have to write instead a page extension and introduce it there with additional code?
     
     
    BR
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 77,925 Super User 2025 Season 1 on at
    Dynamic date filtering in a AL query
    Hi, I think the simplest method is the following:
    You can call the SetFilter and SetRange method from the AL code of the Business Central object that runs the query object or from the OnBeforeOpen Trigger of the query object.
     
    Hope this can give you some hints.
    Thanks.
    ZHU

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans