Skip to main content

Notifications

Small and medium business | Business Central, N...
Unanswered

How can I use changecompany with a query

Posted on by 20

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?

Categories:
  • Lateralus Profile Picture
    Lateralus 20 on at
    RE: How can I use changecompany with a query

    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")
                {
                }
  • SeGra Profile Picture
    SeGra 44 on at
    RE: How can I use changecompany with a query

    You can use StartSession for this.

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

  • HappyBoy59 Profile Picture
    HappyBoy59 11 on at
    RE: How can I use changecompany with a query

    change company for the source table onBeforeOpenRun trigger then run

  • Lateralus Profile Picture
    Lateralus 20 on at
    RE: How can I use changecompany with a query

    I don't think you have read the question.

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

  • HappyBoy59 Profile Picture
    HappyBoy59 11 on at
    RE: How can I use changecompany with a query

    You need use change company for source table like

    GLAccount.ChangeCompany('Test');

  • Lateralus Profile Picture
    Lateralus 20 on at
    RE: How can I use changecompany with a query

    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. :-(

  • YUN ZHU Profile Picture
    YUN ZHU 73,339 Super User 2024 Season 2 on at
    RE: How can I use changecompany with a query

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,228 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans