Deleting purchase order orphan records in Ax 2009

Job to delete purchase order orphan records.

    TimeInMS                                endTime;
    TimeInMS                                startTime;

    InventTransId                           inventTransId;
    InventQtyMarked                         inventQtyMarked;
    InventTransRefId                        inventTransRefId;

    InventDim                               inventDimJoin;
    InventDim                               inventDimCriteria;
    InventDimParm                           inventDimParmCriteria;

    DataArea                                dataAreaLoc;
    PurchLine                               purchLineLoc;
    InventTrans                             inventTransLoc;

    InventMovement                          inventMovement;
    InventUpd_Picked                        inventUpdPicked;
    InventUpd_Estimated                     inventUpdEstimated;
    InventSumRecalcItem                     inventSumRecalcItem;

    SysConsistencyCheckJob                  systemConsistencyCheckBase;
    InventConsistencyCheck_Trans            inventConsistencyCheckTrans;
    InventConsistencyCheck_Onhand           inventConsistencyCheckOnHand;
    ;

    startTime = WinAPI::getTickCount();

    while select dataAreaLoc
        where dataAreaLoc.isVirtual == NoYes::No
    {
        if (xDataArea::exist(dataAreaLoc.id))
        {
            changeCompany(dataAreaLoc.id)
            {
                purchLineLoc            = null;
                inventTransLoc          = null;
                inventDimJoin           = null;
                inventDimCriteria       = null;
                inventDimParmCriteria   = null;

                while select purchLineLoc
                    index hint StatusItemIdx
                    where purchLineLoc.PurchStatus  != PurchStatus::Backorder
                        && purchLineLoc.ItemId      != ''
                {
                    inventTransId       = purchLineLoc.InventTransId;
                    inventTransRefId    = purchLineLoc.PurchId;
                    inventQtyMarked     = InventTrans::qtyMarked(inventTransId);

                    if (InventTrans::qtyTotalOrderPick(inventTransId))
                    {
                        select forupdate inventTransLoc
                        index hint TransIdIdx
                        order by StatusIssue,InventDimId
                            where inventTransLoc.InventTransId      == inventTransId
                                &&  inventTransLoc.StatusReceipt    == StatusReceipt::Ordered
                                &&  inventTransLoc.StatusIssue      == StatusIssue::None
                                &&  inventTransLoc.TransChildType   == InventTransChildType::None
                                &&  inventTransLoc.TransChildRefId  == ''
                                &&  inventTransLoc.TransType        == InventTransType::Purch
                                &&  inventTransLoc.TransRefId       == inventTransRefId
                        #InventDimExistsJoin(inventTransLoc.inventDimId,inventDimJoin,inventDimCriteria,inventDimParmCriteria);

                        if (inventTransLoc)
                        {
                            ttsbegin;

                                // Deleting inventory transaction
                                inventTransLoc.delete(NoYes::Yes);

                                info ("Inventory on-hand recalculation...");
                                inventSumRecalcItem = new InventSumRecalcItem(inventTransLoc.ItemId, true, CheckFix::Check);
                                inventSumRecalcItem.updateNow();

                                // Consistency Check - Inventory Transaction
                                systemConsistencyCheckBase = new SysConsistencyCheckJob(classidget(inventConsistencyCheckTrans));

                                if (systemConsistencyCheckBase)
                                {
                                    info ("Consistency check for inventory transactions...");
                                    systemConsistencyCheckBase.init();

                                    systemConsistencyCheckBase.init();
                                    systemConsistencyCheckBase.initTmp();
                                    systemConsistencyCheckBase.parmCheckFix(CheckFix::Check);

                                    systemConsistencyCheckBase.run();
                                }


                                // Consistency Check - Inventory On-Hand
                                systemConsistencyCheckBase = new SysConsistencyCheckJob(classidget(inventConsistencyCheckOnHand));

                                if (systemConsistencyCheckBase)
                                {
                                    info ("Consistency check for inventory on-hand...");
                                    systemConsistencyCheckBase.init();
                                    systemConsistencyCheckBase.initTmp();
                                    systemConsistencyCheckBase.parmCheckFix(CheckFix::Check);

                                    systemConsistencyCheckBase.run();
                                }

                            ttscommit;

                            info (strfmt("Purchase order %1 processed in company %2", purchLineLoc.PurchId, curExt()));
                        }
                    }
                }
            }
        }
    }

    endTime = WinAPI::getTickCount();

    info (strfmt("Total processing time %1 milli seconds", (endTime-startTime)));

Comments

Popular posts from this blog

Dynamics 365 Finance & Operations Release pipelines import.

Attaching a user selected document to a purchase order using x++ job (Runnable class) in Dynamics 365 for Finance and Operations

Claude AI Models Comparison (Updated: July 2026): Haiku 4.5 vs Sonnet 5 vs Opus 4.8 vs Fable 5