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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Suggested Answer

How to cancel a confirmed Purchase Order in X++

(0) ShareShare
ReportReport
Posted on by 1,424

Hi,

How to cancel a confirmed Purchase Order along with all the lines in X++?

Anas.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Andrew Xu Profile Picture
    3,877 on at

    Hi Anas,

    To cancel a purchase order,

       args = new Args();

       args.record([PurchTable]);

       args.parmEnumType(enumNum(NoYes));

       args.parmEnum(NoYes::Yes);

       PurchCancel::main(args);

    If you just want to reset the  'Approval Status' of that purchase order back to 'Approved' from 'Confirmed', you can simply update the DocumentState field value to 'Approved'.

  • Suggested answer
    ksv1 Profile Picture
    555 on at

    Hi muhammed

    follow the below procedure for purchase order i think that will solve your issue

    ramdynamicsax.wordpress.com/.../how-to-get-cancel-status-to-a-sales-order-or-purchase-order-in-ms-dynamics-ax-2012

  • Suggested answer
    Community Member Profile Picture
    on at

    You can do this by two ways in x++ given below.

    Method 1:

    static void purchCancel(Args _args)

    {

       // purchtable.purchid =='000039'

       PurchTable purchtable;

       PurchLine purchLine;

       PurchPrepayTable purchPrepayTable;

       PurchQty  purchQty;

       boolean   hasLines = false;

       boolean   messageShown = false;

       PdsCWInventQty  pdsCWQty;

       MarkupTrans         markupTrans;

       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 ==  '000039'

           && purchPrepayTable.IsDeleted == NoYes::No;

       if (purchPrepayTable)

       {

           if (purchPrepayTable.PrepayApplicationRemaining == 0.0)

           {

               //reverses the prepayment invoices which have not been paid or settled

               PurchPrepayTable::reverseUnpaidPostedAdvances('000039');

               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

               error(strFmt("@SYS341265", 000039));

               return;

           }

       }

       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             == '000039'

              && 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;

                   if (purchLine.isDropShipment() &&

                       purchTable.InterCompanyDirectDelivery == NoYes::Yes)

                   {

                       purchTable.InterCompanyDirectDelivery = NoYes::No;

                       purchTable.doUpdate();

                   }

               }

               InventMovement::bufferSetTransQtyUnit(purchLine);

               InventMovement::bufferSetRemainQty(purchLine);

               // now cancel the markup

               while select forupdate * from markupTrans

                   where markupTrans.TransRecId == purchTable.RecId

                      && markupTrans.TransTableId == purchLine.TableId

                      && markupTrans.MarkupCategory == MarkupCategory::Fixed

                      && !markupTrans.IsDeleted

               {

                   markupTrans.Value = 0;

                   markupTrans.update();

               }

               purchLine.update(false, true, false);

           }

       }

       if (!hasLines)

       {

           purchTable.PurchStatus = PurchStatus::Canceled;

           purchTable.update();

       }

       if ( purchTable.MCRDropShipment )

       {

           mcrOrderEventTable.insertFromDropShipment(purchTable.referenceSalesTable().SalesId, '000039', MCROrderEventType::CancelDropShipPO);

       }

       ttscommit;

       PurchPrepayTable::checkApplicationRemaining(purchTable.PurchId, purchTable.InvoiceAccount);

       info('done');

    }

    Method 2:

    1) Make a class and extends from PurchCancel class

    2) PurchCancel purchCancel;

       purchCancel = PurchCancel::construct();

       purchCancel.parmPurchTable(_purchTable);

       purchCancel.run();

    Please share results. Thanks

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Sanhthosh.Kumar.K Profile Picture

Sanhthosh.Kumar.K 2

#2
Raed Salah Bzour Profile Picture

Raed Salah Bzour 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans