Thursday, August 10, 2023

Create Transfer order using X++

public void createTransferOrder(InventLocationId _inventLocationIdFrom,InventLocationId  _inventLocationIdTo, TransDate _deliveryDate)
    {
        inventTransferTable.clear();
        inventTransferTable.initValue();

        InventTransferId transferOrderId = InventTransferTable::numberSeq().num();
        inventTransferTable.TransferId = transferOrderId;

        inventTransferTable.InventLocationIdFrom = _inventLocationIdFrom;
        inventTransferTable.modifiedField(fieldNum(InventTransferTable, InventLocationIdFrom));
        inventTransferTable.InventLocationIdTo = _inventLocationIdTo;
        inventTransferTable.modifiedField(fieldNum(InventTransferTable, InventLocationIdTo));
        inventTransferTable.InventLocationIdTransit = InventLocation::find(inventTransferTable.InventLocationIdFrom).InventLocationIdTransit;
        inventTransferTable.DeliveryDateControlType = SalesDeliveryDateControlType::None;
        inventTransferTable.ShipDate = _deliveryDate;
        inventTransferTable.ReceiveDate = _deliveryDate;

        inventTransferTable.initFromAddress();
        inventTransferTable.initToAddress();
        inventTransferTable.AutoReservation = true;

        if (inventTransferTable.validateWrite())
        {
            inventTransferTable.insert();
        }
        else
        {
            throw error("@SCM:FailTransferOrder");
        }
    }

    
    /// <summary>
    /// Create transfer order lines
    /// </summary>
    /// <param name = "_currPurchLine">PurchLine table buffer</param>
    /// <param name = "_inventTransferTable">Transfer order header buffer</param>
    public void createTransferLine(PurchLine _currPurchLine, InventTransferTable _inventTransferTable)
    {
        InventTransferLine currInventTransferLine;
        
        currInventTransferLine.initValue();
        
        currInventTransferLine.ItemId		= _currPurchLine.ItemId;

        currInventTransferLine.initFromInventTable(InventTable::find(_currPurchLine.ItemId));
        currInventTransferLine.initFromInventTableModule(InventTableModule::find(currInventTransferLine.ItemId, ModuleInventPurchSales::Invent));
        currInventTransferLine.initFromInventTransferTable(_inventTransferTable, true);

        currInventTransferLine.inventdimid	= _currPurchLine.InventDimId;
        
        currInventTransferLine.QtyTransfer = _currPurchLine.QtyOrdered;
        currInventTransferLine.QtyRemainReceive = currInventTransferLine.QtyTransfer;
        currInventTransferLine.QtyRemainShip    = currInventTransferLine.QtyTransfer;

        currInventTransferLine.DeliveryDateControlType = SalesDeliveryDateControlType::None;

        TransDate shipdate = this.getShipDate(_currPurchLine);
       
        currInventTransferLine.ShipDate = shipdate;
        InventItemInventSetup inventItemInventSetup = InventItemInventSetup::find(currInventTransferLine.itemId, currInventTransferLine.inventDimId);

        if (inventItemInventSetup.LeadTime)
        {
            currInventTransferLine.ReceiveDate =  currInventTransferLine.ShipDate + inventItemInventSetup.LeadTime;
        }
        else
        {
            currInventTransferLine.modifiedField(fieldNum(InventTransferLine, ShipDate));
        }

        currInventTransferLine.LineNum = _currPurchLine.LineNumber;

        
        if (currInventTransferLine.validateWrite())
        {
            currInventTransferLine.insert();

            info(strFmt("@SCM:CreateTOfromSalesLine", currInventTransferLine.TransferId, currInventTransferLine.ItemId));
           
        }
        else
        {
            throw error(strFmt("@SCM:FailedToCreateTtransferLineFromSalesLine", currInventTransferLine.TransferId, currInventTransferLine.ItemId));
        }
    }

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8