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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

How can I use changecompany with a query

(0) ShareShare
ReportReport
Posted on by 35

I have a query that sums up some general ledger entries, because it is faster to use a query than to use a findset repeat until contruct.

But in Company X I want to change to company Y, and then read the query, but I cannot figure out how to change the record in the query. It still runs for Company X.

the query looks like this

query 50000 "MyQuery"
{
    Access = Internal;
    Caption = 'MyQuery';
    QueryType = Normal;
    DataAccessIntent = ReadOnly;

    elements
    {
        dataitem(GLAccount; "G/L Account")
        {
            column(No_; "No.")
            { }
            column(Account_Type; "Account Type")
            {
            }
            column(IncomeBalance; "Income/Balance")
            {
            }
            filter(Dimension_Set_ID_Filter; "Dimension Set ID Filter")
            {
            }

            dataitem(GLEntry; "G/L Entry")
            {
                DataItemLink = "G/L Account No." = GLAccount."No.";

                column(G_L_Account_No_; "G/L Account No.")
                { }
                column(G_L_Account_Name; "G/L Account Name")
                { }
                column(Dimension_Set_ID; "Dimension Set ID")
                { }
                column(Amount; Amount)
                {
                    Method = Sum;
                }
                filter(Posting_Date; "Posting Date")
                {
                }

            }
        }
    }

    trigger OnBeforeOpen()
    begin

    end;

}

Any ideas?

  • YUN ZHU Profile Picture
    103,132 Super User 2026 Season 2 on at

    Hi, As far as I know, there is no company property in Query. When using it, you can call the ChangeCompany method before opening it and it should work.

    More details:

    Record.ChangeCompany Method:

    https://yzhums.com/6362/

    pastedimage1686526485522v1.png

    Query.Open Method:

    https://yzhums.com/4869/

    Hope this helps.

    Thanks.

    ZHU

  • TG-29091321-0 Profile Picture
    35 on at

    This does not work, as you are only changing company for the CompInfo variable but not for the Query itself.

    Even though I try what you outline, it still runs in the company the user is in. :-(

  • Premkumar0509 Profile Picture
    44 on at

    You need use change company for source table like

    GLAccount.ChangeCompany('Test');

  • TG-29091321-0 Profile Picture
    35 on at

    I don't think you have read the question.

    How would you change company for a table in a query. It is not possible

  • Premkumar0509 Profile Picture
    44 on at

    change company for the source table onBeforeOpenRun trigger then run

  • SeGra Profile Picture
    44 on at
  • TG-29091321-0 Profile Picture
    35 on at

    You cannot do that because you cannot create a variable for the record that you use in the dataitem

    Here I am not able to create a var for "G/L Account" as a global or local variable that I can use in the query

    query 50000 "MyQuery"
    {
        Access = Internal;
        Caption = 'MyQuery';
        QueryType = Normal;
        DataAccessIntent = ReadOnly;

        elements
        {
            dataitem(GLAccount; "G/L Account")
            {
                column(No_; "No.")
                { }
                column(Account_Type; "Account Type")
                {
                }
                column(IncomeBalance; "Income/Balance")
                {
                }
                filter(Dimension_Set_ID_Filter; "Dimension Set ID Filter")
                {
                }

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 Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 674 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 362 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 359 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans