AX – D365FO – Cross-Company in Select statement in X++ and AOT

You can create a cross-company query by using X++ code. There are two ways to do this:

  • crossCompany keyword on the select statement
  • Query class methods

A cross-company query can also be created under the Query node in the Application Object Tree (AOT).

A cross-company query cannot return data for companies that the user does not have permission to access. A cross-company query can be limited to a subset of the companies that the user is authorized to access.

The cross-company query is useful if at least one of the tables has the SaveDataPerCompany property set to Yes.

crossCompany Keyword

You can create a cross-company query by using the crossCompany keyword on the X++ select statement. You have the option of adding a container variable of company identifiers immediately after the crossCompany keyword (separated by a colon). The container restricts the selected rows to those with a dataAreaId that match a value in the container.

The following code example populates a table buffer with all the BankAccountTable rows that have a dataAreaId of either cm1 or cm2 or dat. This example assumes that the user has authority to access data for these three companies. The first two dataAreaId values that are found will be printed.X++Copia

static void JobDemoCrossCompany(Args _args)
    {
        BankAccountTable tabBAT; // saveDataPerCompany == true.
        container conCompanies = [ 'cm1', 'cm2', 'dat' ];
        str 4 sCompanyPrevious = " "; // Maximum length is 4 characters.
        int iCountCompanies = 0;
        ;
        while select
            crossCompany
                : conCompanies
            * from tabBAT
            order by dataAreaId
        {
            if ( sCompanyPrevious != tabBAT.dataAreaId )
            {
                info( tabBAT.dataAreaId + " = tabBAT.dataAreaId" );
                iCountCompanies++;
                if ( iCountCompanies >= 2 )
                {
                    break;
                }
                sCompanyPrevious = tabBAT.dataAreaId;
            }
        }
        return;
    }

Query Class

In X++ code you can use the Query .allowCrossCompany property method to achieve the same result as you can with the crossCompany keyword on a select statement. The calls to the Query .addCompanyRange method are the same as appending a container of companies to the crossCompany keyword.

You cannot perform data source level filtering by company in a cross-company query. This is why the call to qbds3 .company is commented out in the following code example.

 static void JobDemoAllowCrossCompany(Args _args)
    {
        BankAccountTable tabBAT; // saveDataPerCompany == true.
        Query qry2;
        QueryBuildDataSource qbds3;
        QueryRun qrun4;
        str sCompanyPrevious = "   ";
        int iCountCompanies = 0;
        int iTableNumBAT;
        ;
        qry2 = new Query();
        qry2.allowCrossCompany( true );
        qry2.addCompanyRange( 'dat' );
        qry2.addCompanyRange( 'twf' );
        
        iTableNumBAT = tableNum( BankAccountTable );
        qbds3 = qry2 .addDataSource( iTableNumBAT );
        //qbds3.company( 'dat' );
        
        qrun4 = new QueryRun( qry2 );
        
        while ( qrun4.next() )
        {
            if ( qrun4.changed( iTableNumBAT ) )
            {
                tabBAT = qrun4.get( iTableNumBAT );
        
                if ( sCompanyPrevious != tabBAT.dataAreaId )
                {
                    print( tabBAT.dataAreaId + " = tabBAT.dataAreaId" );
                    iCountCompanies++;
                    if ( iCountCompanies >= 2 )
                    {
                        break;
                    }
                    sCompanyPrevious = tabBAT.dataAreaId;
                }
            }
        }
        pause;
        return;
    }

Query in the AOT

In the AOT, you can get cross-company functionality from a query node by setting the AllowCrossCompany property to Yes (the equivalent of true in X++ code). You can override methods on the node of your query to call the Query.addCompanyRange method.

For more information, see Cross-Company Reports in the AOT.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...