Version of Microsoft Dynamics AX: D365O
1. Selects
Using intercompany functionality to fetch data from all companies at once, not just from the current company, as in the case with a regular select, use keyword crosscompany.

To select data not from all companies at once, but only from some of them, use keyword crosscompany and specified a list of companies:


Another way to fetch data from a company other than the current company is to “switch” to another company using changecompany. In this case, all the code is executing in the context of the specified company.

2. Forms data sources
If you need to fetch data from multiple companies at the same time in the form data source, you need to change the value of property Cross Company Auto Query to «Yes». In this case, the data will be selecting from all existing companies.

To limit the list of companies, you need it set the values of the required companies in the init() method of this data source.


3. Copying records
To copy a record from one company to another, you need to “switch” to the desired company using changecompany and initialize all the fields (except system fields) of the new record with buf2buf function.
Below is the code for a method that creates a new record and initializes its fields with the values of the original record:

As an example of the standard buf2buf function using you can see the class CustFreeInvoiceCorrection.

In cases where, for some reason, you to save the values of the system fields of the original record, except for field DataAreaId, since this field must in any case correspond to the company in which the new record is created, use the table data method.
At the same time, when creating a new record, some of the system fields can be overridden, and field DataAreaId must be overridden.
Overriding system fields must be allowed by special construction with using OverwriteSystemfieldsPermission().assert() и overwriteSystemfields().
Here is an example of code that overrides system fields in the new record.

4. Intercompany Sales Purchase orders
An example of the standard Intercompany functionality is association between purchase order in one company and sales order in another company.
Having a purchase order, you can get an intercompany sales order and having a sales order, you can get an intercompany purchase order.
Below are custom examples of receiving sales and purchase orders.


Here is an example of standard code in MarkupCopy class that gets intercompany sales order..
