web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to cancel a Purchase Order in X++

(0) ShareShare
ReportReport
Posted on by 125

Can anybody tell me how to cancel a purhase order with purchStatus  Open Order and Document status None 

in X++

*This post is locked for comments

I have the same question (0)
  • Vilmos Kintera Profile Picture
    46,149 on at

    That means there is nothing confirmed, and you should be able to just delete the PO when you are on the header.

    ttsBegin;
    purchTable = PurchTable::find('yourpurchid', true);
    purchTable.delete();
    ttsCommit;
  • Suggested answer
    Raheel-Khan Profile Picture
    1,371 on at

    Hi Faran,

    Can you please explain how you want to cancel your PO? using "Deliver remainder function"?

    In AX2012, You can close/cancel PO by using Deliver remainder function method described in the link mentioned below:

    http://dynamicsteaching.com/how-to-cancel-deliver-remainder-in-ax-2012/

    Regards,

    Raheel

  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at

    Hi!

    This is a custom class. The class has run(), cancelMarkup() and parmPurchTable() methods.

    private void cancelMarkup(
        TableId     _transTableId,
        RecId       _transRecId
        )
    {
        MarkupTrans         markupTrans;
    
        while select forupdate * from markupTrans
            where markupTrans.TransRecId == _transRecId
               && markupTrans.TransTableId == _transTableId
               && markupTrans.MarkupCategory == MarkupCategory::Fixed
               && !markupTrans.IsDeleted
        {
            markupTrans.Value = 0;
            markupTrans.update();
        }
    }

    /// <summary>
    /// Performs the purchase order cancelation.
    /// </summary>
    public void run()
    {
        PurchLine purchLine;
        PurchPrepayTable purchPrepayTable;
        PurchQty  purchQty;
        boolean   hasLines = false;
        boolean   messageShown = false;
        PdsCWInventQty  pdsCWQty;
        // <GBR>
        boolean   clearFiscalDocumentRef_BR = false;
        NoYes     isBrazilEnabled = BrazilParameters::isEnabled();
        // </GBR>
        McrOrderEventTable mcrOrderEventTable;
    
        ttsbegin;
    
        //selects a prepayment definition which does not have a paid or settled prepayment invoice
        select firstonly forupdate PrepayApplicationRemaining from purchPrepayTable
            where purchPrepayTable.PurchTable == purchTable.PurchId
            && purchPrepayTable.IsDeleted == NoYes::No;
    
        if (purchPrepayTable)
        {
            if (purchPrepayTable.PrepayApplicationRemaining == 0.0)
            {
                //reverses the prepayment invoices which have not been paid or settled
                PurchPrepayTable::reverseUnpaidPostedAdvances(purchTable.PurchId);
                purchPrepayTable.Value = 0.0;
                purchPrepayTable.PrepayLimit = 0.0;
                purchPrepayTable.PrepayAvailable = 0.0;
                purchPrepayTable.Description = '';
                purchPrepayTable.PrepayCategoryId = 0;
                purchPrepayTable.update();
            }
            else
            {
                //throws error and returns with message that a paid prepayment invoice exists therefore purchase order cannot be cancelled
                throw error(strFmt("@SYS341265", purchTable.PurchId));
            }
        }
    
        if (purchTable.ChangeRequestRequired && purchTable.DocumentState >= VersioningDocumentState::Approved)
        {
            if (!purchTable.selectForUpdate())
            {
                purchTable = PurchTable::findRecId(purchTable.RecId, true);
            }
            VersioningPurchaseOrder::newPurchaseOrder(purchTable).createChangeRequest();
        }
        else if (!VersioningPurchaseOrder::newPurchaseOrder(purchTable).isLastVersionArchived() && purchTable.DocumentState == VersioningDocumentState::Confirmed)
        {
            // force archiving to avoid it during line cancellation as that would lead to update conflicts.
            purchTable.update();
        }
    
        // do not cancel invoice matched lines.
        while select forupdate purchLine
            where purchLine.PurchId             == purchTable.PurchId
               && purchLine.IsDeleted           == NoYes::No
        {
            hasLines = true;
            if (purchLine.IsInvoiceMatched == NoYes::Yes)
            {
                if (purchLine.PurchStatus != PurchStatus::Invoiced)
                {
                    if (!messageShown)
                    {
                        info("@SYS342633");
                        messageShown = true;
                    }
                    info(strFmt("@SYS342634", purchLine.LineNumber, purchLine.ItemId));
                }
            }
            else
            {
                if (pdsIsCWItem(purchLine.ItemId))
                {
                    pdsCWQty = purchLine.pdsCWReceivedInTotal() + purchLine.pdsCWArrived() + purchLine.pdsCWRegistered();
                }
                else
                {
                    pdsCWQty = 0;
                }
                purchQty = purchLine.receivedInTotal() + purchLine.arrivedInPurchUnit() + purchLine.registeredInPurchUnit();
                if (purchQty)
                {
                    // if anything was recieved, arrived or registered then write down to what was recieved, arrived and registered.
                    purchLine.PurchQty      = purchQty;
                    purchLine.PdsCWQty      = pdsCWQty;
                    purchLine.LineAmount    = purchLine.calcLineAmountForced();
                }
                else
                {
                    purchLine.PurchQty      = 0;
                    purchLine.LineAmount    = 0;
                    purchLine.PdsCWQty      = 0;
                    purchLine.PurchStatus = PurchStatus::Canceled;
                    // <GBR>
                    if (isBrazilEnabled)
                    {
                        clearFiscalDocumentRef_BR = true;
                    }
                    // </GBR>
                    if (purchLine.isDropShipment() &&
                        purchTable.InterCompanyDirectDelivery == NoYes::Yes)
                    {
                        purchTable.InterCompanyDirectDelivery = NoYes::No;
                        purchTable.doUpdate();
                    }
                }
    
                InventMovement::bufferSetTransQtyUnit(purchLine);
                InventMovement::bufferSetRemainQty(purchLine);
    
                // now cancel the markup
                this.cancelMarkup(purchLine.TableId, purchLine.RecId);
    
                purchLine.update(false, true, false);
            }
        }
    
        // cancel header markup
        this.cancelMarkup(purchTable.TableId, purchTable.RecId);
    
        if (!hasLines)
        {
            purchTable.PurchStatus = PurchStatus::Canceled;
            purchTable.update();
        }
    
        // <GBR>
        if (isBrazilEnabled && (clearFiscalDocumentRef_BR || !hasLines))
        {
            purchTable.removeInvoiceRefRecId_BR();
        }
         // </GBR>
    
        if ( purchTable.MCRDropShipment )
        {
            mcrOrderEventTable.insertFromDropShipment(purchTable.referenceSalesTable().SalesId, purchTable.PurchId, MCROrderEventType::CancelDropShipPO);
        }
    
        ttscommit;
    
        PurchPrepayTable::checkApplicationRemaining(purchTable.PurchId, purchTable.InvoiceAccount);
    }
  • Suggested answer
    udaY-ch Profile Picture
    5,173 on at

    Hi,

    Try following the link below

    daxbeginners.wordpress.com/.../how-to-cancel-deliver-remainder-through-x

    Best,

    Uday.

  • Faran Baig Profile Picture
    125 on at

    Scenario is to change the status to cancel,not to delete the PO,actually i was provided with excel file containing PO's, and i have to change their PurchStatus to "cancel" after verifying  that PO has purchStatus  "Open Order" and Document status "None".can i do this in job in X++?if yes,suggest me the code.

  • Rustem Galiamov Profile Picture
    8,072 on at

    I suggest the code above.

  • Faran Baig Profile Picture
    125 on at

    Scenario is just to change the PurchStatus to "Cancel" after verifying that PO has purchStatus  "Open Order" and Document status "None".

    I am provided by excel sheet containing 1572 PO's. and i have to run my scenario on that PO's.

    If you can help me by solution regarding job in X++ .

  • Faran Baig Profile Picture
    125 on at

    Scenario is just to change the PurchStatus to "Cancel",not to delete PO, after verifying that PO has purchStatus  "Open Order" and Document status "None".

    I am provided by excel sheet containing 1572 PO's. and i have to run my scenario on that PO's.

    If you can help me by solution regarding job in X++ .

  • Verified answer
    Rustem Galiamov Profile Picture
    8,072 on at

    Create your own class using my suggestion and call it from job inside “while select” statement.

  • Faran Baig Profile Picture
    125 on at

    Thanks  Rustem Galiamov.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
Henrik Nordlöf Profile Picture

Henrik Nordlöf 2 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans