Updating Batch Tracked Items with Blank Batches

For a time we had an issue where batch tracked items were being brought into our system with blank batches. I wrote the job below to look for the transactions by item, find the InventDim and correlating InventBatch records, and update them accordingly.


static void fixBlankBatches(Args _args)
{
    InventBatch         batch;
    InventBatchId       batchIdBlank = "";
    InventBatchId       batchInitial = "INITIAL";
    InventTable         inventTable;
    ItemId              itemId = "109534";
    InventDim           inventDim;
    InventDim           newDim;

    InventTrans         trans;
    ;

    while select forupdate trans
        join inventDim
        where inventDim.inventDimId == trans.inventDimId
        && trans.ItemId == itemId
        && inventDim.inventBatchId == batchIdBlank
    {
        newDim.clear();

        newDim.initFromInventDim(inventDim);
        newDim.InventLocationId = inventDim.InventLocationId;
        newDim.InventSiteId = inventDim.InventSiteId;
        newDim.inventBatchId = batchInitial;
        newDim = InventDim::findOrCreate(newDim);

        ttsbegin;
        trans.inventDimId = newDim.inventDimId;
        trans.update();
        ttscommit;

        info(strfmt("Order: %1, InventTransId: , updated.", trans.TransRefId, trans.InventTransId));
    }

    info("Done.");
}