Introduction
In Dynamics AX and Dynamics 365 Finance and Operations (D365FO), restoring deleted sales orders is essential for maintaining accurate records and ensuring business continuity. When a sales order is deleted, it can be stored in a backup table from which it can be restored if needed. The provided code snippet demonstrates how to restore a deleted sales order by retrieving it from the backup table (SalesTableDelete and SalesLineDelete), and then reinserting it into the original tables (SalesTable and SalesLine).
X++ Code
...
static void restoreDeletedSO(Args _args)
{
SalesTableDelete
backupSalesTable;
SalesLineDelete
backupSalesLine;
SalesTable restoredSalesTable;
SalesLine restoredSalesLine;
str deletedSalesId = 'SO/240001;
// Example Sales ID to be restored
;
// Find the
deleted SalesTable record using the Sales ID
backupSalesTable =
SalesTableDelete::find(deletedSalesId, true);
// Begin the
transaction block
ttsbegin;
// Check the
status of the deleted sales order
switch
(backupSalesTable.Cancelled)
{
// If the
sales order is voided
case
Voided::Voided :
//
Retrieve the SalesTable record from the container and insert it back
restoredSalesTable = conpeek(backupSalesTable.SalesTable, 1);
restoredSalesTable.insert();
//
Retrieve and insert all associated SalesLine records
while
select forupdate backupSalesLine where backupSalesLine.SalesId ==
backupSalesTable.SalesId
{
restoredSalesLine = conpeek(backupSalesLine.SalesLine, 1);
restoredSalesLine.insert();
}
// Delete
the restored SalesTableDelete record
backupSalesTable.delete();
break;
// If only the
sales order lines are voided
case
Voided::linesVoided :
//
Retrieve, insert, and delete each SalesLine record
while
select forupdate backupSalesLine where backupSalesLine.SalesId ==
backupSalesTable.SalesId
{
restoredSalesLine = conpeek(backupSalesLine.SalesLine, 1);
restoredSalesLine.insert();
backupSalesLine.delete();
}
// Delete
the SalesTableDelete record
backupSalesTable.delete();
break;
}
// Commit the
transaction block
ttscommit;
}
...
Restoring deleted sales orders in Dynamics AX and Dynamics 365 Finance and Operations (D365FO) is a critical operation for maintaining the integrity and accuracy of your sales data. The provided code snippet demonstrates a structured approach to achieve this by utilizing the backup tables (SalesTableDelete and SalesLineDelete) to reinstate the deleted records into the active tables (SalesTable and SalesLine).