Hi everyone.
I use the OData connected services library to create sales orders and sales order lines to Dynamics 365. This works well for the most part. My functionality first saves a sales order, then once that has been saved, it saves its sales order lines.
However, if an error occurs on saving the sales order lines, the sales order lines are obviously not saved, but the sales order has already been saved. I have tried to delete the sales order when there is an error saving the lines, but I was unsuccessful. This is my code:
try
{
// code for creating sales order and sales order lines
}
catch (Exception ex)
{
try
{
if (salesOrderCreated)
{
foreach (SalesOrderLine line in salesOrderLineCollection)
{
d365.DeleteObject(line);
}
d365.DeleteObject(soh);
d365.SaveChanges();
}
}
catch (Exception e)
{
throw e;
}
}
d365 is the Context of the d365 object of type Resources.
Anyone know how I can delete the sales order successfully? With the current implementation, I am getting the following error on saving the changes after deleting soh:
Precondition Failed
at Microsoft.OData.Client.SaveResult.HandleResponse() in D:\a\1\s\src\Microsoft.OData.Client\SaveResult.cs:line 347
at Microsoft.OData.Client.BaseSaveResult.EndRequest() in D:\a\1\s\src\Microsoft.OData.Client\BaseSaveResult.cs:line 304
at Microsoft.OData.Client.DataServiceContext.SaveChanges(SaveChangesOptions options) in D:\a\1\s\src\Microsoft.OData.Client\DataServiceContext.cs:line 2260
at Microsoft.OData.Client.DataServiceContext.SaveChanges() in D:\a\1\s\src\Microsoft.OData.Client\DataServiceContext.cs:line 2239
at myCode.CreateSalesOrder(Resources d365, SalesOrder) in C:\path:line 205
I have tried different ways to resolve this, such as creating a different context and saving using that, or detaching and reattaching the sales order before saving. I was unsuccessful with both these attempt.