Dynamic grouping for inventory dimension on SSRS Report in AX12 using query builder

We usually see the inventory dimensions selection criteria while running several inventory reports. I just went through the requirement where we needed to group on dimensions dynamically based on the selection. Different approaches can be used. I used query builder classes. Let’s say a user wants to split data on site, warehouse and batch number for the item

Below is the code which is getting a sum of quantity from the inventory transaction table for the items.

     Query                 inventTransQuery = new Query();
    QueryBuildRange       dynRange;
    QueryBuildDataSource  qbdsInvent, qbdsDim;
   
    inventTransQuery.clearAllFields();
 
    qbdsInvent  = inventTransQuery.addDataSource(tableNum(InventTrans));
    qbdsDim     = qbdsInvent.addDataSource(tableNum(InventDim));
 
    qbdsInvent.addSelectionField(fieldNum(InventTrans,Qty), SelectionField::Sum);
    qbdsInvent.addGroupByField(fieldNum(InventTrans,ItemId));
    qbdsInvent.addSelectionField(fieldNum(InventTrans, ItemId));
    qbdsDim.relations(true);
 
 
Now, adding dynamic grouping for inventory dimensions.

 
    inventdimviewcontract = contract.parmInventDimViewContract();
 
    if (inventdimviewcontract.parmViewInventSiteId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, InventSiteId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, InventSiteId));
    }
 
    if (inventdimviewcontract.parmViewInventLocationId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, InventLocationId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, InventLocationId));
    }
 
    if ( inventdimviewcontract.parmViewInventBatchId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, InventBatchId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, InventBatchId));
    }
 
    if (inventdimviewcontract.parmViewInventSerialId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, InventSerialId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, InventSerialId));
    }
 
    if (inventdimviewcontract.parmViewInventSizeId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, InventSizeId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, InventSizeId));
    }
 
    if (inventdimviewcontract.parmViewInventColorId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, InventColorId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, InventColorId));
    }
 
    if (inventdimviewcontract.parmViewConfigId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, ConfigId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, ConfigId));
    }
 
    if (inventdimviewcontract.parmViewWMSLocationId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, wMSLocationId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, wMSLocationId));
    }
 
    if (inventdimviewcontract.parmViewInventStyleId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, InventStyleId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, InventStyleId));
    }
 
    if (inventdimviewcontract.parmViewWMSPalletId())
    {
        _qbdsDim.addSelectionField(fieldNum(inventDim, WMSPalletId));
        _qbdsDim.addOrderByField(fieldNum(inventDim, WMSPalletId));
    }
 
    _qbdsDim.orderMode(OrderMode::GroupBy);

AX Query:

 SELECT SUM(Qty), ItemId FROM InventTrans
GROUP BY InventTrans.ItemId, InventDim.InventSiteId, InventDim.InventLocationId, InventDim.inventBatchId
JOIN InventSiteId, InventLocationId, inventBatchId
FROM InventDim
ON InventTrans.inventDimId = InventDim.inventDimId

 SQL Query:
SELECT SUM(Qty), ItemId, InventSiteId, InventLocationId, inventBatchId
FROM InventTrans
JOIN InventDim
ON InventTrans.inventDimId = InventDim.inventDimId
GROUP BY InventTrans.ItemId, InventDim.InventSiteId, InventDim.InventLocationId, InventDim.inventBatchId


 

 

 

 

 


Comments

Popular posts from this blog

Batch jobs stuck on WAITING status in AX 2012

Electronic Reporting For Beginners

Computed column and virtual field in D365