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

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
    95,739 Super User 2025 Season 2 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

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 2,135

#2
YUN ZHU Profile Picture

YUN ZHU 733 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 612

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans