Thursday, January 30, 2025

Reserve and unreserve Item using X++

public void reserveItem(ItemId _itemId,InventTransId  _inventTransId, Purchline _purchline)
{
    InventTrans             inventTrans;
    InventTransOrigin       inventTransOrigin;
    InventMovement          inventMovement;
    InventUpd_Reservation   inventUpd_Reservation ;

    InventDim inventdim;

    // Reserve an item
    select inventTrans
        where  inventTrans.ItemId                   == _itemId
        &&  inventTrans.StatusReceipt               == StatusReceipt::None
        &&  inventTrans.StatusIssue                 == StatusIssue::OnOrder
        exists join inventTransOrigin
        where   inventTransOrigin.RecId            == inventTrans.InventTransOrigin
        && inventTransOrigin.InventTransId == _inventTransId
        && inventTrans.MarkingRefInventTransOrigin == InventTransOrigin::findByInventTransId(_purchline.InventTransId).RecId;

    inventdim = inventTrans.inventDim();

    inventdim.inventBatchId = strFmt("%1-%2",_purchline.PurchId,_purchline.InventTransId );

    inventdim = inventdim::findDim(inventdim);

    if(inventTrans.RecId)
    {
        Inventmovement = inventTrans.inventmovement(true);
        inventUpd_Reservation = InventUpd_Reservation::newInventDim(inventmovement,inventdim ? inventdim : inventTrans.inventDim(),inventTrans.Qty, false);
        inventUpd_Reservation.updatenow();
    }
}

public void removeReserveItem(ItemId _itemId,InventTransId  _inventTransId)
{
    InventTrans             inventTrans;
    InventTransOrigin       inventTransOrigin;
    InventMovement          inventMovement;
    InventUpd_Reservation   inventUpd_Reservation ;

    // Remove reservations and markings on a reserved transfer order
    while select inventTrans
        where  inventTrans.ItemId                      == _itemId
        &&  inventTrans.StatusReceipt               == StatusReceipt::None
        && (inventTrans.StatusIssue                 == StatusIssue::ReservPhysical
        ||  inventTrans.StatusIssue                 == StatusIssue::ReservOrdered)
        exists join inventTransOrigin
        where   inventTransOrigin.RecId            == inventTrans.InventTransOrigin
        && inventTransOrigin.InventTransId == _inventTransId
    {
        if (inventTrans.StatusIssue == StatusIssue::ReservPhysical || inventTrans.StatusIssue == StatusIssue::ReservOrdered)
        {
            Inventmovement = inventTrans.inventmovement(true);
            inventUpd_Reservation = InventUpd_Reservation::newInventDim(inventmovement,inventTrans.inventDim(), -1 * inventTrans.Qty, false);
            inventUpd_Reservation.updatenow();
        }
    }
}

Trigger Sysoperation through button click

class PurchTableForm_EventHandler
{
    
    /// <summary>
    /// run business logic for ConnectTransferOrder button
    /// </summary>
    /// <param name="sender">sender</param>
    /// <param name="e">e</param>
    [FormControlEventHandler(formControlStr(PurchTable, ConnectTransferOrder), FormControlEventType::Clicked),SuppressBPWarning('BPParameterNotUsed', 'Parameter required')]
    public static void ConnectTransferOrder_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormDataSource formds = sender.formRun().dataSource(formDataSourceStr(PurchTable, PurchTable));
        PurchTable purchTable = PurchTable::findRecId(formds.cursor().RecId);

        CreateUpdateTransferOrderController controller;
        SysOperationStartResult sysOperationStartResult;
    
        Args args = new Args();

        args.caller(sender.formRun());
        args.record(purchTable);

        controller = CreateUpdateTransferOrderController::newFromArgs(args);
        controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);

        sysOperationStartResult = controller.startOperation();

        formds.refresh();
        formds.reread();
    }

}

Table browser URL in D365FO

Critical Thinking icon icon by Icons8