September 27, 2012

How to Restore deleted sales order or purchase order

How to Restore deleted sales order or purchase order in AX 2009






Hi Frnds,

Some times fortunately or unfortunately user delete a record from Purchase Order OR Sales Order Forms,
and it can be drag you in a big problems, Here I am sharing a trick to restore these deleted Order records.
When ever a record is deleted from a Form, it sits in the void tables. These orders can be viewed from

AR -> Inquiries -> History -> Voided sales order or AP -> Inquiries -> History -> Voided Sales order.

AP -> Inquiries -> History -> Voided sales order or AP -> Inquiries -> History -> Voided Purchase order.

Now from this form you can only view the deleted orders but there is not option of restoring them.
Below are some sample code, you can use this to restore the voided orders.

Sales order restoration:
static void restoreDeletedSO(Args _args)
{
    SalesTableDelete    salesTableDelete;
    SalesLineDelete     salesLineDelete;
    SalesTable          salesTable;
    SalesLine           salesLine;
    ;

    SalesTableDelete = SalesTableDelete::find('00450_036', true);
    ttsbegin;
    switch (salesTableDelete.Cancelled)
    {
        case Voided::Voided :
            salesTable  = conpeek(salesTableDelete.SalesTable, 1);
            salesTable.insert();

            while select forupdate salesLineDelete where salesLineDelete.SalesId == salesTableDelete.SalesId
            {
                salesLine = conpeek(salesLineDelete.SalesLine, 1);
                salesLine.insert();
            }
            salesTableDelete.delete();
            break;

        case Voided::linesVoided :
            while select forupdate salesLineDelete where salesLineDelete.SalesId == salesTableDelete.SalesId
            {
                salesLine = conpeek(salesLineDelete.SalesLine, 1);
                salesLine.insert();
                salesLineDelete.delete();
            }
            salesTableDelete.delete();
            break;
   }

   ttscommit;
}


Purchase order restoration:





static void restoreDeletedPO(Args _args)
{
    PurchTableDelete    purchTableDelete;
    PurchLineDelete     purchLineDelete;
    PurchTable          purchTable;
    PurchLine           purchLine;
    ;

    purchTableDelete = PurchTableDelete::find('00242_049', true);
    ttsbegin;
    switch (purchTableDelete.Cancelled)
    {
        case Voided::Voided :
            purchTable  = conpeek(purchTableDelete.PurchTable, 1);
            purchTable.insert();

            while select forupdate purchLineDelete where purchLineDelete.PurchId == purchTableDelete.PurchId
            {
                purchLine = conpeek(purchLineDelete.PurchLine, 1);
                purchLine.insert();
            }
            purchTableDelete.delete();
            break;

        case Voided::linesVoided :
            while select forupdate purchLineDelete where purchLineDelete.PurchId == purchTableDelete.PurchId
            {
                purchLine = conpeek(purchLineDelete.PurchLine, 1);
                purchLine.insert();
                purchLineDelete.delete();
            }
            purchTableDelete.delete();
            break;
   }

   ttscommit;
}



If you have any query for the same feel free to ask.  
-Harry

5 comments:

  1. Hi does the solution apply to Navision as well?

    ReplyDelete
  2. i didnt try this. Please try and share your feedback....

    ReplyDelete
  3. But where this procedure should be used, can you tell me the steps

    ReplyDelete
    Replies
    1. Hi Yuusaf,

      I am not getting you query. Please share some more details.
      IF you want to use this code , than put this code in a job and update require parameters.

      Delete
  4. Will this solution also work in AX 2012 R2?

    ReplyDelete

Thanks