Monday, September 1, 2025

Cancle the partial quantity of purch order using x++ d365fo

purchTable = PurchTable::find(purchId, true);

if (purchTable != null)

{

    // Proceed only if PO is in "Confirmed" state

    if (purchTable.DocumentState == VersioningDocumentState::Confirmed)

    {

        try

        {

            ttsbegin;

            // Loop through all purchase lines for the given PO

            while select forupdate * from purchLine

                where purchLine.PurchId == purchTable.PurchId

            {

                // Check if line is not received/invoiced and can be cancelled

                if (purchLine && purchLine.receivedInTotal() == 0 && purchLine.invoicedInTotal() == 0)

                {

                    purchLine.selectForUpdate(true);

                    purchLine.reread();

                    // Reset purchase quantity to 0

                    purchLine.PurchQty = 0.00;

                    purchLine.update();

                    // Update inventory buffer accordingly

                    InventMovement::bufferSetRemainQty(purchLine);

                }

            }

            // Update purchase order header status

            purchTable.PurchStatus  = PurchStatus::Canceled;

            purchTable.ReqAttention = "Test";   // Custom flag for follow-up

            purchTable.update();


            // Post cancellation through PurchFormLetter (PurchaseOrder)

            purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);


            purchFormLetter.update( purchTable,purchTable.PurchId,

                DateTimeUtil::getSystemDate(DateTimeUtil::getCompanyTimeZone()), // Posting date

                PurchUpdate::All,       // Cancel all lines

                AccountOrder::None,     // No specific vendor account order

               false,               // Whether this is proforma

                false,                // Whether to print PO

                false                 // Whether to reprint PO

            );

            ttscommit;

        }

        catch (Exception::Error)

        {

            // Rollback transaction on error

            ttsabort;

            error(strFmt("Error while cancelling purchase order %1", purchId));

        }

    }

}


Call API using x++ D365FO

 System.Net.HttpWebRequest    request; System.Net.HttpWebResponse   response; System.IO.Stream             dataStream; System.IO.StreamReade...