Thursday, October 29, 2020

Marking via X++

X++ code for marking in D365FO (it is somewhat different from that in AX 2012):

    InventTransId         issueInventTransId   = 'x';
    InventTransId         receiptInventTransId = 'y';
 
    InventTransOriginId receiptInventTransOriginId = 
        InventTransOrigin::findByInventTransId(receiptInventTransId).RecId;
    InventTrans         receiptInventTrans         = 
        InventTrans::findByInventTransOrigin(receiptInventTransOriginId);
 
    InventTransOriginId issueInventTransOriginId = 
        InventTransOrigin::findByInventTransId(issueInventTransId).RecId;
    InventTrans         issueInventTrans       = 
        InventTrans::findByInventTransOrigin(issueInventTransOriginId);
 
    collection = TmpInventTransMark::markingCollection(
        InventTransOrigin::find(receiptInventTransOriginId),
        receiptInventTrans.inventDim(),
        receiptInventTrans.Qty);
 
    collection.insertCollectionToTmpTable(tmpInventTransMark);
 
    select firstonly tmpInventTransMark
        where tmpInventTransMark.InventTransOrigin == issueInventTrans.InventTransOrigin
           && tmpInventTransMark.InventDimId       == issueInventTrans.InventDimId;
 
    if (tmpInventTransMark.RecId != 0)
    {
        Qty qtyToMark = issueInventTrans.Qty;
 
        tmpInventTransMark.QtyMarkNow =  qtyToMark;
        tmpInventTransMark.QtyRemain  -= tmpInventTransMark.QtyMarkNow;
 
        mapUpdated = new Map(Types::Int64, Types::Record);
        mapUpdated.insert(tmpInventTransMark.RecId, tmpInventTransMark);
 
        TmpInventTransMark::updateTmpMark(
                receiptInventTransOriginId,
                receiptInventTrans.inventDim(),
                -qtyToMark,
                mapUpdated.pack());
    }