Skip to main content

Notifications

Community site session details

Community site session details

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

Use queries as variables within procedures

(1) ShareShare
ReportReport
Posted on by 30
Hello,
 
Making use of 'Query.Open' method (explained more in detail here > query as a data source for a page), I was trying to set up a page, where users can use action triggers to jump between various queries. Although, I succeeded to reach that goal, the code is quite repetitive, as I cannot really make use of one local procedure to bundle such calls for an easier maintenance.  
 
To explain, what I mean, I did the following:
  • Setting up temporary table
    • name: "InvoiceQueryStatus"
    • fields: "No.", "Job No.", "Amount"
  • Setting up the queries
    • name1: "Purchase Invoice Overdue"
    • name2: "Sales Invoice Overdue" 
    • remark: both with identical column names
  • Setting up the page as in the following
page 50001 InvoiceStatus
{
    ApplicationArea = All;
    PageType = List;
    SourceTable = InvoiceQueryStatus;
    UsageCategory = ReportsAndAnalysis;

    layout
    {
        area(Content)
        {
            repeater(General)
            {
                field(No; Rec."Job No.")
                {}
                field(Amount; Rec.Amount)
                {}
            }
        }
    }
    actions
    {
        area(Processing)
        {
            action("Purchase Overdue")
            {
                trigger OnAction()
                var
                    InvoiceQuery: Query "Purchase Invoice Overdue";
                begin
                    if Rec.Next() > 0 then begin
                        Rec.DeleteAll();
                    end;
                    if InvoiceQuery.Open() then begin
                        while InvoiceQuery.Read() do begin
                            Rec.Init();
                            Rec."No." := Rec."No." + 1;
                            Rec."Job No." := InvoiceQuery.JobNo;
                            Rec.Amount := InvoiceQuery.Amount;
                            Rec.Insert();
                        end;
                        InvoiceQuery.Close();
                    end;
                end;
            }
            action("Sales Overdue")
            {
                trigger OnAction()
                begin
                    SalesOverdue();
                end;
            }
        }
    }

    trigger OnOpenPage()
    begin
        SalesOverdue();
    end;

    local procedure SalesOverdue()
    var
        InvoiceQuery: Query "Sales Invoice Overdue";
    begin
        if Rec.IsEmpty() <> true then begin
            Rec.DeleteAll();
        end;
        if InvoiceQuery.Open() then begin
            while InvoiceQuery.Read() do begin
                Rec.Init();
                Rec."No." := Rec."No." + 1;
                Rec."Job No." := InvoiceQuery.JobNo;
                Rec.Amount := InvoiceQuery.Amount;
                Rec.Insert();
            end;
            InvoiceQuery.Close();
        end;
    end;
}
As seen above the code of the local procedure is identical with the code within the action block ... with the one exception that its variable 'InvoiceQuery' differs in its type. Therefore, my question if it is somehow possible to use a condition to make of either the one or the other query?
 
 
BR
  • Suggested answer
    YUN ZHU Profile Picture
    84,621 Super User 2025 Season 1 on at
    Use queries as variables within procedures
    Hi, As far as I know, there is no good way to do this, since this is not a table.
    If you want users to choose to open it, it is recommended to use the following way.
    Dynamics 365 Business Central: QueryCategory property – Smartlist? (Execute Queries from Pages)
     
    Although it is not a good method, you can also open Query directly using the following method.
    Hyperlink(GetUrl(ClientType::Current, CompanyName, ObjectType::Query, Rec."Object ID"));
     
    Hope this helps.
    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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 1,452

#2
YUN ZHU Profile Picture

YUN ZHU 1,313 Super User 2025 Season 1

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 1,083 Most Valuable Professional

Featured topics

Product updates

Dynamics 365 release plans