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

Announcements

News and Announcements icon
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
I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    102,023 Super User 2026 Season 1 on at
    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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,690 Super User 2026 Season 1

#2
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 1,091 Super User 2026 Season 1

#3
YUN ZHU Profile Picture

YUN ZHU 1,047 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans