-1

I have a cross-company property ("Yes") enabled query.,even though query returns values for the current company in Dynamics 365 (AX7)

I even tried with changecompany() but there is no data for any other companies apart from current company.

_query.allowCrossCompany(true);
 qr=new QueryRun(_query);
while(qr.next()) 
{ 
companyInfo =qr.get(tableNum(CompanyInfo));
info(strfmt("%1",companyInfo .DataArea));
} 

here query is cross company query but still it showing the values for current company only(ie, if I run this code from USA dataarea shows only USA not other companies)

  • Please see [this help](https://stackoverflow.com/help/mcve) as the question in its current form is difficult to answer, e.g. you tagged the question with multiple versions of AX - did you try your same query on AX 2012 and D365O and have the same problem? – DAXaholic Aug 30 '17 at 13:04
  • i tried in both ax 2012 and D365. but it is not working – Bharathi Karthikeyan Aug 30 '17 at 13:13
  • What is your question? Please show your code. – Jan B. Kjeldsen Aug 30 '17 at 15:03
  • while(qr.next()) { companyInfo =qr.get(tableNum(CompanyInfo));info(strfmt("%1",companyInfo .DataArea)); } // here query is cross company query but still it showing the values for current company only(ie, if I open run this code from USA dataarea shows only USA not other companies) – Bharathi Karthikeyan Aug 30 '17 at 16:09
  • Can you show how _query is created? Also CompanyInfo table is not a company specific table. So please explain what you are trying to do too. – Pradeep Muttikulangara Vasu Aug 30 '17 at 16:18
  • Try doing `info(qr.toString())` and see if that gives you any more information about what's wrong. – Alex Kwitny Aug 31 '17 at 16:40

1 Answers1

1

First, CompanyInfo is SaveDataPerCompany = No, so cross company serves no purpose.

Second, you must not be providing all of your code, because I just created/tried both of these jobs and they worked fine for crossCompany. Job12 is the one similar to what you did, but it still works.

static void Job11(Args _args)
{
    Query           query   = new Query();
    QueryRun        qr;
    SalesTable      salesTable;

    query.addDataSource(tableNum(SalesTable));
    query.allowCrossCompany(true);

    qr      = new QueryRun(query);

    while(qr.next()) 
    {
        salesTable = qr.get(tableNum(salesTable));
        info(strFmt("%1 %2", salesTable.SalesId, salesTable.dataAreaId));
    } 
}


static void Job12(Args _args)
{
    Query           query   = new Query();
    QueryRun        qr;
    CompanyInfo     companyInfo;

    query.addDataSource(tableNum(CompanyInfo));
    query.allowCrossCompany(true);

    qr      = new QueryRun(query);

    while(qr.next()) 
    {
        companyInfo = qr.get(tableNum(CompanyInfo));
        info(strfmt("%1", companyInfo.DataArea));
    } 
}
Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71
  • thank you alex! but what if any of the table in my query has "SaveDataPerCompany = No" .,will I get values for only current company? – Bharathi Karthikeyan Aug 31 '17 at 06:53
  • 1
    It shouldn't matter. If you enable cross company and use a mix of `SaveDataPerCompany= yes & no` it should just display all records. From the actual SQL query, it simply removes the `&& dataAreaId='abc'` – Alex Kwitny Aug 31 '17 at 21:50