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:
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(); } } } } |
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(); } } } } } |
are created in GLRT and USRT companies:
Advanced job results are:
No comments:
Post a Comment