I have created an order and would like to change the order status, I have tried setting the status before and after the order is Finalized, but its not being saved.
Here is the code:
using (var uow = _uniteOfWorkProvider.Create())
{
var order = _sessionManager.GetOrCreateCurrentOrder(store.Id).AsWritable(uow);
var orderStatus = _orderStatusService.GetOrderStatus(store.Id, "newReturnOrder");
order.SetOrderStatus(orderStatus);
_orderService.SaveOrder(order);
uow.Complete();
}
I can see that the order object is getting updated, but its not being saved.
The code on it's own seems correct. Where is this code running though? in a controller? or event handler?
Generally if something doesn't persist my first port of call would be to check all unit's of work have uow.Complete() called on them as if you have any event handlers that don't do this, it can prevent a whole pipeline from persisting.
Ok, well, these really all should be performed within a single unit of work and using a single writable order to update all of these.
By using individual units of work you have the potential that you can leave your system an a middle state if early UoW's succeed, but then the later ones fail. The point of a UoW is to act as a transaction and ensure everything happens, or nothing happens.
I think your potential issue here might be that you are finalizing an order in an early stage, and then using GetOrCreateCurrentOrder in a later stage, however because you have finalized your order, this method will give you a new order. Once an order is finalized, if you need to get it from the session manager again, you need to call GetCurrentFinalizedOrder instead.
As I say though, if these were all in one UoW, you can be sure that the order is the same order throughout.
Thanks Matt, using GetCurrentFinalizedOrder instead did the trick. Got tunnelvision and forgot about that method...
I had all of the code in the same uow in the beginning, but then most of it did not get saved, so split them out and it worked. Will test some more on using one uow.
It would probably be best to try and work out why it didn't work in the single UoW really, but if what you have works, just know you can get into an inconsistent state should any one UoW fail. If you are happy with that, then cool. I just need to point it out and make it clear to yourself and anyone else who has a similar problem.
Update order status
I have created an order and would like to change the order status, I have tried setting the status before and after the order is Finalized, but its not being saved.
Here is the code:
I can see that the order object is getting updated, but its not being saved.
Hi Kenni,
The code on it's own seems correct. Where is this code running though? in a controller? or event handler?
Generally if something doesn't persist my first port of call would be to check all unit's of work have
uow.Complete()
called on them as if you have any event handlers that don't do this, it can prevent a whole pipeline from persisting.Hi Matt,
It's running in a SurfaceController which I'm using to create return orders.
I'm have a few unit's of work running for: (Had to split them else they would not save either)
They all have
uow.Complete();
called on them the order is created correctly except for the status still being new.Are all those actions occurring at the same time just within their own units of work?
They are all within the same try catch, but are running in sequence
All other snippets are saving the data correct, only the status that is being ignored.
Ok, well, these really all should be performed within a single unit of work and using a single writable order to update all of these.
By using individual units of work you have the potential that you can leave your system an a middle state if early UoW's succeed, but then the later ones fail. The point of a UoW is to act as a transaction and ensure everything happens, or nothing happens.
I think your potential issue here might be that you are finalizing an order in an early stage, and then using
GetOrCreateCurrentOrder
in a later stage, however because you have finalized your order, this method will give you a new order. Once an order is finalized, if you need to get it from the session manager again, you need to callGetCurrentFinalizedOrder
instead.As I say though, if these were all in one UoW, you can be sure that the order is the same order throughout.
Thanks Matt, using
GetCurrentFinalizedOrder
instead did the trick. Got tunnelvision and forgot about that method...I had all of the code in the same uow in the beginning, but then most of it did not get saved, so split them out and it worked. Will test some more on using one uow.
It would probably be best to try and work out why it didn't work in the single UoW really, but if what you have works, just know you can get into an inconsistent state should any one UoW fail. If you are happy with that, then cool. I just need to point it out and make it clear to yourself and anyone else who has a similar problem.
Yea I understand that, got it running in one uow now. Using
_orderService.SaveOrder(order)
after every update instead of using separate uow.is working on a reply...