I'm currently trying to update orders out of a scheduled job.
The code runs through without any problems/errors but the order itself isn't updated. I don't have any error logs either.
The code which updates the order looks like this:
using var uow = _vendrApi.Uow.Create();
var writableOrder = orderSearchResult.AsWritable(uow);
writableOrder.SetOrderStatus(successOrderStatus);
writableOrder.SetProperties(new Dictionary<string, string>
{
{ Constants.Checkout.Order.ApiResponse.BookingCode, bookingResponse.BookingCode },
{ Constants.Checkout.Order.ApiResponse.IdentificationId, bookingResponse.IdentificationId },
});
_orderService.SaveOrder(writableOrder);
uow.Complete();
This code runs for every order which is in a specific failed state as we have to send them over a third party api. The job runs every 30min and grabs every order in the specific state like this:
var orderSearchResults = _vendrApi
.SearchOrders(x =>
x.FromStore(store.Id).And(x.HasOrderStatus(failedOrderStatus.Id)), itemsPerPage: 100);
Any hints what could cause the saving to not happen?
Usually where there is no error it's often because there is something somewhere creating a UoW that doesn't get completed and so chain of units fails and is rolled back.
Do you have any event handlers or anything that could be failing to call complete on a UoW?
so I tried again today. The DB entry hasn't updated after I tried it.
Strange thing I found is that I could add a tag to the order.
And that saved perfectly.
Will try a bit more. Little strange behaviour that the tag is saved.
Ok one more test and it seems that setting the order status is the issue.
I could execute the code this way and the order was saved without issue. When I set the new order status nothing happens.
So this works:
using var uow = _vendrApi.Uow.Create();
var writableOrder = orderSearchResult.AsWritable(uow);
writableOrder.RemoveTag("Test");
//writableOrder.SetOrderStatus(successOrderStatus);
writableOrder.SetProperties(new Dictionary<string, string>
{
{ Constants.Checkout.Order.ApiResponse.BookingCode, "bla!" },
{ Constants.Checkout.Order.ApiResponse.IdentificationId, "identification bla!" },
});
_vendrApi.SaveOrder(writableOrder);
uow.Complete();
WIll try adding a handler and see if that gives some clues.
Ahhh, I think the issue might be that your SearchOrder is searching unfinalized orders too (you'll need to add to the where clause if you only want finalized orders) and then I think this is causing an issue I've seen somewhere else (which has recently been fixed) in that, our activity logger would log when an order status changed, but it assumed only finalized orders would have their status changed. Because you are pulling back all orders (even carts) you are probably changing the status of some unfinalized orders and so that is causing the activity logger to fail.
You'll either need to filter your search further to exclude unfinalized orders, or if you need to handle carts too, you might need to remove the LogOrderStatusChangeActivity domain event (let me know if you need this and I'll shared the code to do it as Domain events aren't meant to be "changed" by users)
Can't update order from scheduled job
Hi Matt,
I'm currently trying to update orders out of a scheduled job. The code runs through without any problems/errors but the order itself isn't updated. I don't have any error logs either.
The code which updates the order looks like this:
This code runs for every order which is in a specific failed state as we have to send them over a third party api. The job runs every 30min and grabs every order in the specific state like this:
Any hints what could cause the saving to not happen?
Regards David
Hi David,
It looks OK to me.
Usually where there is no error it's often because there is something somewhere creating a UoW that doesn't get completed and so chain of units fails and is rolled back.
Do you have any event handlers or anything that could be failing to call complete on a UoW?
Matt
Hi Matt,
there are no event handlers that we use. At least none from Vendr or where we try to manipulate an order.
Every other place where we change properties or status, everything is saved properly. Is there a way to see if the UoW isn't completed properly?
David
Unfortunately our UoW (currently) wraps Umbraco's IScope and it doesn't really give you much info as to why a scope was ended.
Have you looked in the DB to see if the order was actually updated and maybe it's just the cache that is stale?
Hi Matt,
so I tried again today. The DB entry hasn't updated after I tried it. Strange thing I found is that I could add a tag to the order. And that saved perfectly.
Will try a bit more. Little strange behaviour that the tag is saved.
David
Hmm, that is weird.
Maybe create an event handler for "order saving" or something and do some extra logging to see if you have something overwriting it for some reason?
Ok one more test and it seems that setting the order status is the issue. I could execute the code this way and the order was saved without issue. When I set the new order status nothing happens.
So this works:
WIll try adding a handler and see if that gives some clues.
Ahhh, I think the issue might be that your
SearchOrder
is searching unfinalized orders too (you'll need to add to the where clause if you only want finalized orders) and then I think this is causing an issue I've seen somewhere else (which has recently been fixed) in that, our activity logger would log when an order status changed, but it assumed only finalized orders would have their status changed. Because you are pulling back all orders (even carts) you are probably changing the status of some unfinalized orders and so that is causing the activity logger to fail.You'll either need to filter your search further to exclude unfinalized orders, or if you need to handle carts too, you might need to remove the
LogOrderStatusChangeActivity
domain event (let me know if you need this and I'll shared the code to do it as Domain events aren't meant to be "changed" by users)Matt
Hi Matt,
sorry for the late response, I was on holiday last week.
I tried the NotificationHandler to see if everything is set correctly. If I change the status of the order the "saved" one is never called.
I checked and the order is correctly set to finalized.
So I guess for whatever reason the domain event prevents the order from being saved. Not sure why.
Can you point me on how to remove or change the domain event so I can maybe check what is causing the problem.
David
Hey David,
That's very strange if the order is finalized. You can try removing the domain even with the following
Let me know if that works.
Matt
Thanks Matt!
That did the trick and now everything is updating. Not sure what prevents it when the domain event is active but I take it for now. :)
is working on a reply...