Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

display other company's data in current company

Posted on by 944

Hi all,

I am using AX2009 and I am trying to display other company's data in current company, I use the setDefaultCompany method to change company and open the form, but it didn't work well, it cannot show the destination company's data.

The current company is "XYZ", I want to show "ABC" company's data, The code like:

Args parameters = new Args();
FormRun formR;

;

appl.setDefaultCompany("ABC", false);

parameters.caller(element);
parameters.record(_record);
formR = new MenuFunction(menuItemDisplayStr(LedgerJournalTable3), MenuItemType::Display).create(parameters);
formR.run();
formR.wait(true);

appl.setDefaultCompany("XYZ", false);

I appreciate you can show me what's wrong, the settings, coding or others?

Thanks and Regards,

Tat

*This post is locked for comments

  • Verified answer
    Kenneth Won Profile Picture
    Kenneth Won 944 on at
    RE: display other company's data in current company

    Hi all,

    Thanks for all of your replies. I fixed it.

    The factor is the variable "_record" in the below code.

    The variable "_record" is selected in company "XYZ", then I change company to "ABC", unfortunately, I pass the variable "_record" to the Form and hope it can display what I want. This is my fault, so I select the "_record" after changed company to ABC, the problem fixed.

    Args parameters = new Args();
    FormRun formR;

    ;

    appl.setDefaultCompany("ABC", false);

    parameters.caller(element);
    parameters.record(_record);
    formR = new MenuFunction(menuItemDisplayStr(LedgerJournalTable3), MenuItemType::Display).create(parameters);
    formR.run();
    formR.wait(true);

    appl.setDefaultCompany("XYZ", false);

  • Maciej Obojski Profile Picture
    Maciej Obojski 610 on at
    RE: display other company's data in current company

    Hi,

    I do not have access to AX 2009 to check, but changecompany() approach definitely works in AX 2012 - even Microsoft uses it here:

    \Forms\EcoResProductAvailability\Designs\Design\[Group:GridContainer]\[Grid:Grid]\StringEdit:InventTable_ItemId\Methods\jumpRef

    Thanks,

    Maciej

  • Suggested answer
    Divya Lakshmi Profile Picture
    Divya Lakshmi 745 on at
    RE: display other company's data in current company

    Hi,

    Suggestion from Martin is absolutely right.

    Changecompany will not support ur scenario.

    Retrieving data using Crosscompany will be the Best practice.

    Regards,

    Divya Lakshmi.J

  • Suggested answer
    Maciej Obojski Profile Picture
    Maciej Obojski 610 on at
    RE: display other company's data in current company

    Hi,

    why don't you use changecompany instead of appl.setdefaultcompany?

    I have tried your code and it works fine:

    changeCompany('ABC')

       {

           formR = new MenuFunction(menuItemDisplayStr(LedgerJournalTable3), MenuItemType::Display).create();

           formR.run();

           formR.wait(true);

       }

    Thanks,

    Maciej

  • Kenneth Won Profile Picture
    Kenneth Won 944 on at
    RE: display other company's data in current company

    Hi all,

    Thanks for all of your replies.

    I hundred percent sure that the issue of my case is come from the Form: LedgerJournalTable, I tried to launch the Form: PurchTable, it works well. When launch the Form: LedgerJournalTable , it snap back to the current company, something like what Brandon Wiese said.

    Again, Thank you very much.

    Best Regards,

    Tat

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: display other company's data in current company

    John is correct that you can use crossCompany keyword in select statements, but it's not what you want if you're building a form.

    You have to switch the CrossCompanyAutoQuery property on your datasource to Yes, or use a query with AllowCrossCompany = Yes. Then you can filter DataAreaId to see data just from some companies. Refer to MSDN: Cross-Company Data Access for details.

    Don't use changeCompany() for this scenario and definitely not use the code snippet by Jignesh Patel, where he loads all records from the table without reason. It would be a performance killer.

  • Suggested answer
    Brandon Wiese Profile Picture
    Brandon Wiese 17,786 on at
    RE: display other company's data in current company

    You're trying to launch a form in the context of another company.  Using appl.setDefaultCompany(..) is the appropriate way to do this, because you're involving UX elements like forms, but you're not checking the return value of setDefaultCompany(..) which can return false for a number of reasons, such as user security.  Check the return value, as it may explain why your code doesn't work, although then you'll want to figure out why it fails in your case.

    The use of changeCompany(..) does not work well with UX elements like lookups or forms.  I've seen it work, but then it can sometimes "snap" back to the current company.  The most important consideration for changeCompany(..) is that it ignores user security and can allow users to access and change data in companies they do not otherwise have access to through the UX.

    The query keyword crosscompany is a whole other approach, but it fully respects user security, and if you want to use it with a form data source you could have other challenges, and you're modifying the form directly rather than merely launching it in another context.

  • Suggested answer
    UmesH@ Profile Picture
    UmesH@ 810 on at
    RE: display other company's data in current company

    Or Else Create LookUp and Bind Comapny DataAreaId

    based on that CompanyLookUp you can Bind Company wise data in Others.

    public void lookup()
    {
            Query                   qr = new Query();
            QueryBuildDataSource    qbds;
            QueryBuildDataSource    qbdsJoin;
            QueryBuildRange qbr;
            ProjTable ProjTable;
            QueryRun                qrun;   
            SysTableLookup          sysTableLookup = sysTableLookup::newParameters( tableNum(ProjTable), this);    
    //ProjTable - Table Name
            ;
            qbds= qr.addDataSource( tableNum(ProjTable));
            qbds.joinMode(JoinMode::InnerJoin);
            qbds.relations(true);
            qbds.company(strFmt(DSMJTrans.LegalEntity));
    //DSMJTrans.LegalEntity -DataSourname and Field Name
            qbr = qbds.addRange(fieldNum(ProjTable, DataAreaId));
            qbr.value(queryValue(strFmt(DSMJTrans.LegalEntity)));
            sysTableLookup.parmQuery(qr);
            sysTableLookup.addLookupfield( fieldNum(ProjTable, ProjId));
            sysTableLookup.addLookupfield( fieldNum(ProjTable, Name));
            sysTableLookup.performFormLookup();
    }


    Thanks.

  • Suggested answer
    UmesH@ Profile Picture
    UmesH@ 810 on at
    RE: display other company's data in current company

    If You Want to Retrieve Other Company Data then You Have to Set CrossCompany.

    static void ChangeCompany(Args _args)
    {
    
    CompanyName nameOfCompany;
    TableName contractCompany; // trying to check companycode field from this table
    CompanyInfo company;
    ;
        while select contractCompany
        {
            if (contractCompany.dataAreaId == 'ceu')
            {
                changecompany(contractCompany.LegalEntity)
                {
                    nameOfCompany = curext();
                    //info(strFmt("Name : %1",nameOfCompany));
                }
            }
            if (contractCompany.dataAreaId == 'ceu')
            {
                changecompany(contractCompany.LegalEntity)
                {
                    nameOfCompany =curext();
                    //info(strFmt("Name : %1",nameOfCompany));
                }
            }
            if (contractCompany.dataAreaId == 'ceu')
            {
                changecompany(contractCompany.LegalEntity)
                {
                    nameOfCompany =curext();
    
                }
            }
            info(strFmt("Comapny-Name : %1",nameOfCompany));
        }
    }

    For that You Have to Set changecompany('Companyname') and you are Done.
    or else you can set DataSource level CrossCompanyAutoQuery = Yes.

    Thanks.

  • Johnkrish Profile Picture
    Johnkrish 781 on at
    RE: display other company's data in current company

    Hi Tat,

    for getting other company details while you are in the home company, you can use the method called changecompany() otherwise use a select statement with crosscompany keyword like below code to get different company details.

    container conCompanies = [ 'cmp1', 'cmp2', 'cmp3' ];
    while select crossCompany : conCompanies * from yourtable order by dataAreaId

    hope this helpful.

    Regards,
    Johnkrish


    
                          

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!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans