Showing posts with label cross-company. Show all posts
Showing posts with label cross-company. Show all posts

30 September 2015

How to create missing records in another company

Sometimes in multi-company environment it is required to create records which are missing in another company. The first intention can be to use notexists join and crossCompany keyword to find missing records. Such approach has a problem, which is described in the previous post.

Description
Let's assume that the goal is to create records of company DEMF which are missing in company USRT:
Missing records example
An advanced solution will handle a set of source companies and a set of destination companies.

Steps to reproduce
1. Switch to DEMF company
2. Note existing Buyer groups (Inventory and warehouse management > Setup > Inventory > Buyer groups)
3. Switch to USRT company
4. Note existing Buyer groups
5. Run job
6. Compare Buyer groups

Hints
Look for examples in standard AX by searching while select crossCompany text in methods. Continue reading for the solution.

24 August 2015

Non-documented feature of cross-company keyword

My colleague asked my advise about cross-company query. The goal was to create missing records of customer specific table in another company. The query looked correct, however the tests failed.

Problem description
To make it simple let's assume that records of InventItemGroup table in company USMF must be created in company USRT (provided the records are missing in USRT company). It seems to be possible to use cross-company query like this:
InventItemGroup inventItemGroup, inventItemGroupJoin;
    
while select crossCompany inventItemGroup 
    where inventItemGroup.dataAreaId      == 'USMF'
    notExists join inventItemGroupJoin
    where inventItemGroupJoin.dataAreaId  == 'USRT'
       && inventItemGroupJoin.ItemGroupId == inventItemGroup.ItemGroupId
{
    // create new record in USRT company
}
This approach is wrong though. Are you also curious why? Let's analyse some specifics of cross-company queries.

Steps to reproduce
The following job can be used for tests and analysis:
static void CrossCompanyTest(Args _args)
{
    InventItemGroup inventItemGroup, inventItemGroupJoin;
    
    select generateonly forceLiterals crossCompany ItemGroupId 
        from inventItemGroup
        where inventItemGroup.dataAreaId      == 'USMF'
        notExists join inventItemGroupJoin
        where inventItemGroupJoin.dataAreaId  == 'USRT'
           && inventItemGroupJoin.ItemGroupId == inventItemGroup.ItemGroupId;
    
    info(inventItemGroup.getSQLStatement());
}

Hints
Try to modify predicates of join and use Query class instead of select statement.