D365FO – Get serial reservations from a sales order, sales line or sales parm line – X++


Below shows how to get the serial ids or the current qty that is reserved for a sales order, sales line or sales parm line (picking, packing, etc.. form aka SalesEditLines (form))

static void SalesOrderSerialReservations(Args _args)
{
    SalesLine salesLine;
    InventSum inventSum;
    real reserveQty, totalQty;
    InventDim inventDim;
    InventTrans inventTrans;
    InventTransOrigin inventTransOrigin;
    SalesTable salesTable;
    SalesParmLine salesParmLine;
    InventSerial inventSerial;
    container serialIds;

    //get all of the serial reservations for a specific sales order
   while select * from salesTable
        where salesTable.SalesId == '1000970'
   join salesLine
        where salesLine.SalesId == salesTable.SalesId
   join inventTransOrigin
        where inventTransOrigin.InventTransId == salesLine.InventTransId
   && inventTransOrigin.ReferenceCategory == InventTransType::Sales
   join inventTrans
        where inventTrans.InventTransOrigin == inventTransOrigin.RecId
   join inventDim
        where inventDim.inventDimId == inventTrans.inventDimId
   join inventSum
        where inventSum.InventDimId == inventDim.inventDimId
   {
       //reservation qty
       reserveQty =  inventSum.ReservPhysical  - inventSum.ReservOrdered;
      
       info(strFmt("%1    %2",inventDim.inventSerialId,  reserveQty));
   }
  
   
    //get the serials reservations for a specific sales line
    while select * from salesLine
        where salesLine.RecId == 5637223330
    join inventTransOrigin
        where inventTransOrigin.InventTransId == salesLine.InventTransId
    && inventTransOrigin.ReferenceCategory == InventTransType::Sales
    join inventTrans
        where inventTrans.InventTransOrigin == inventTransOrigin.RecId
    join inventDim
        where inventDim.inventDimId == inventTrans.inventDimId
    join inventSum
        where inventSum.InventDimId == inventDim.inventDimId
    {
         reserveQty =  inventSum.ReservPhysical  - inventSum.ReservOrdered;
         info(strFmt("%1    %2",inventDim.inventSerialId,  reserveQty));
    }

    //get the serial reservations for a specific sales line from the salesparmline
    while select RecId,SalesLineRecId  from salesParmLine
        where salesParmLine.RecId == 5637223330
    join RecId,InventTransId from salesLine
        where salesLine.RecId == salesParmLine.SalesLineRecId
    join RecId, InventTransId, ReferenceCategory from inventTransOrigin
        where inventTransOrigin.InventTransId == salesLine.InventTransId
            && inventTransOrigin.ReferenceCategory == InventTransType::Sales
    join InventTransOrigin, inventDimId from inventTrans
        where inventTrans.InventTransOrigin == inventTransOrigin.RecId
    join inventDimId, inventSerialId from  inventDim
        where inventDim.inventDimId == inventTrans.inventDimId
    join InventDimId, ReservPhysical, ReservOrdered from inventSum
        where inventSum.InventDimId == inventDim.inventDimId
    join inventSerial
        where inventSerial.InventSerialId == InventDim.inventSerialId
    {
        //reservation qty
        reserveQty =  inventSum.ReservPhysical  - inventSum.ReservOrdered;
        //total reservation qty
        totalQty += reserveQty;
        //serial ids that are reserved
        serialIds += inventDim.inventSerialId;
    }
   
    info(strFmt("%1    %2",con2Str(serialIds), totalQty ));
}

Comments