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
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);
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
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
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
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
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.
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.
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.
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.
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
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156