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.

Solution
I used standard AX methods for inspiration:
\Data Dictionary\Tables\SharedCategory\Methods\expType
\Classes\VendPromissoryNotePost\postNextStep
\Classes\CustVendReversePosting\updateNow

The following job creates missing records:
static void createMissingRecords(Args _args)
{
    DataAreaId          sourceCompany = 'DEMF', destinationCompany = 'USRT';
    container           sourceCompanies, destinationCompanies;
    InventBuyerGroup    inventBuyerGroup, inventBuyerGroupExist, inventBuyerGroupNew;
    
    // initialize containers for crossCompany queries
    sourceCompanies += [sourceCompany];
    destinationCompanies += [destinationCompany];
    
    // loop through all source records 
    while select crossCompany:sourceCompanies inventBuyerGroup
    {
        // check whether record exists in destination company 
        select firstonly crosscompany:destinationCompanies RecId from inventBuyerGroupExist 
            where inventBuyerGroupExist.Group == inventBuyerGroup.Group;    
        
        if (inventBuyerGroupExist.RecId == 0)
        {
            // create new record in destination company   
            changeCompany (destinationCompany)
            {
                inventBuyerGroupNew.clear();    
                inventBuyerGroupNew.initValue();
                
                // fill fields
                inventBuyerGroupNew.Group       = inventBuyerGroup.Group;
                inventBuyerGroupNew.Description = inventBuyerGroup.Description;
                
                inventBuyerGroupNew.insert();
            }
        }
    }
}
Let's run the job and check Buyer groups in USRT company:
Create missing records job results

The job can be made more generic and use sets of source and destination companies:
static void createMissingRecordsAdvanced(Args _args)
{
    DataAreaId          destinationCompany;
    container           sourceCompanies, destinationCompanies, checkCompanies;
    InventBuyerGroup    inventBuyerGroup, inventBuyerGroupExist, inventBuyerGroupNew;
    int                 i;
    
    // initialize containers for crossCompany queries
    sourceCompanies += ['DEMF', 'INMF'];
    destinationCompanies += ['GLRT', 'USRT'];
    
    // loop through all source records 
    while select crossCompany:sourceCompanies inventBuyerGroup
    {
        // loop through destination companies
        for (i = 1 ; i <= conLen(destinationCompanies) ; i++)
        {
            // get current destination company
            destinationCompany = conPeek(destinationCompanies, i);
            checkCompanies = [destinationCompany];
            
            // check whether record exists in destination company
            select firstonly crosscompany:checkCompanies RecId from inventBuyerGroupExist 
                where inventBuyerGroupExist.Group == inventBuyerGroup.Group;    
        
            if (inventBuyerGroupExist.RecId == 0)
            {
                // create new record in destination company  
                changeCompany (destinationCompany)
                {
                    inventBuyerGroupNew.clear();    
                    inventBuyerGroupNew.initValue();
                
                    // fill fields
                    inventBuyerGroupNew.Group       = inventBuyerGroup.Group;
                    inventBuyerGroupNew.Description = inventBuyerGroup.Description;
                
                    inventBuyerGroupNew.insert();
                }
            }
        }
    }
}
For example, records of DEMF and INMF companies:
Create missing records advanced example

are created in GLRT and USRT companies:
Create missing records advanced example

Advanced job results are:
Create missing records advanced test

No comments:

Post a Comment