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?

I have the same question (0)
  • YUN ZHU Profile Picture
    99,084 Super User 2026 Season 1 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

    You can use StartSession for this.

    See learn.microsoft.com/.../session-startsession-integer-integer-string-table-duration-method

  • 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,946 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,177 Super User 2026 Season 1

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 555 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans