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...
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
    95,331 Super User 2025 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
    28 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
    28 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

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 3,143

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,694 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans