Good day everyone,
This post has as subject virtual company, if you do not know what it is please read the link Virtual company accounts in Microsoft Dynamics AX [AX 2012].
Well, when you use the changeCompany() functionality and your environment has a configured virtual company it might cause a runtime error like this:
Image may be NSFW.
Clik here to view.
This can be avoided by using changeCompany(table.company()) instead of changeCompany(table.dataAreaId).
static void _VirtualCompany(Args _args) { DataArea dataArea; while select Id from dataArea { changeCompany(dataArea.company()) { // Do something } } }
The table.company() method get and sets the property that indicates a legal entity for the record. Which means, it will always returns the selectable company which can be properly used in a changeCompany() statement.
There’s also another way to avoid it by checking on DataArea table if the table DataAreaId is virtual or not. It works and people use it often but I still recommend to use table.company() for a clean code.
while select Id from dataArea { if (!xDataArea::find(dataArea.id).isVirtual) { changeCompany(dataArea.id) { // Do something } } }
Image may be NSFW.
Clik here to view.

Clik here to view.

Clik here to view.
