Thursday, 18 September 2025

D365FO : Script for update Storage Dimension Group for the Items with no transactions. x++

We have a problem on finance test... The storage dimension group = Mlok is setup to use warehouse management. But we don't have all the necessary information on the item to use warehouse management. So we have created a new storage dimension group called Mlok2, that do not use warehouse management.. But now we have a lot of items with Mlok, that need to be changed to Mlok2, none of them have a transaction. But it is very time-consuming to change manually.


 /// <summary>

/// This Runnable class is used to update the new Storage Dimension group for the item 
/// As the existing dimension does not have all the necessary information on the item to use warehouse management, they have decided to update the dimension to a new one
/// Since it has 400k records custom script is failing due to limitation, after data update this class will be deleted.
/// </summary>
internal final class MK_UpdateStorageDimensionsGroup
{

    /// <summary>
    /// Class entry point. The system will call this method when a designated menu
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        const str OLD_GROUP = 'Mlok';
        const str NEW_GROUP = 'Mlok2';
 
        EcoResStorageDimensionGroup oldGroup, newGroup;
        InventTable                 inventTable;
        EcoResProduct               ecoResProduct;
        EcoResStorageDimensionGroupItem    storageDimGroupItem;
        EcoResStorageDimensionGroupProduct storageGroupProduct;
        InventTrans                 inventTrans;
 
        // Find storage dimension groups
        oldGroup = EcoResStorageDimensionGroup::findByDimensionGroupName(OLD_GROUP);
        newGroup = EcoResStorageDimensionGroup::findByDimensionGroupName(NEW_GROUP);
 
        if (!oldGroup.RecId || !newGroup.RecId)
        {
            throw error(strFmt("Storage dimension groups %1 or %2 not found", OLD_GROUP, NEW_GROUP));
        }
 
        // Select only InventTable linked to OLD_GROUP (via item or product)
        while select inventTable
            join ecoResProduct
                where ecoResProduct.RecId == inventTable.Product
            outer join storageDimGroupItem
                where storageDimGroupItem.ItemId == inventTable.ItemId
                    && storageDimGroupItem.StorageDimensionGroup == oldGroup.RecId
            outer join storageGroupProduct
                where storageGroupProduct.Product == ecoResProduct.RecId
                    && storageGroupProduct.StorageDimensionGroup == oldGroup.RecId
                    && inventTable.ItemType == ItemType::Item
        {
            if (!ecoResProduct.RecId)
                continue;
 
            if (storageGroupProduct.RecId || storageDimGroupItem.RecId)
            {
                // Skip items with transactions
                select firstonly RecId from inventTrans
                    where inventTrans.ItemId == inventTable.ItemId;
 
                if (inventTrans.RecId)
                {
                    info(strFmt("Item %1 skipped because it has transactions", inventTable.ItemId));
                    continue;
                }
 
                ttsBegin;
                if (storageGroupProduct.RecId)
                {
                    storageGroupProduct.selectForUpdate(true);
                    storageGroupProduct.StorageDimensionGroup = newGroup.RecId;
                    storageGroupProduct.update();
                }
 
                if (storageDimGroupItem.RecId)
                {
                    storageDimGroupItem.selectForUpdate(true);
                    storageDimGroupItem.StorageDimensionGroup = newGroup.RecId;
                    storageDimGroupItem.update();
                }
 
                ttsCommit;
                //To wrap everything in a single ttsBegin/ttsCommit. If it hit an error midway it roll back everything. Better to commit per item.
                info(strFmt("Item %1 updated from %2 → %3",inventTable.ItemId, OLD_GROUP, NEW_GROUP));
            }
        }
        info("Storage dimension group update completed.");
    }
}

D365FO : Script for update Storage Dimension Group for the Items with no transactions. x++

We have a problem on finance test... The storage dimension group = Mlok is setup to use warehouse management. But we don't have all the ...