Try our conversational search powered by Generative AI!

How to change order status in code?


commerce ver:12.5

Hi Team,

I trying to change the order status using the below code but status is not changed still shows as inprogress.

 var purchaseOrder = orderRepository.SaveAsPurchaseOrder(cart);
                var order = orderRepository.Load(purchaseOrder.OrderGroupId);

var purchaseOrderProcessor = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance();

I checked in commerce manager order status is not changed. Anything I missed? Please help me out.



Edited, Aug 07, 2018 10:08

Did you save the order after these operations?

Aug 07, 2018 10:28

OrderStatusManager works with PurchaseOrder, not IPurchaseOrder. You would need to use the try 2 approach, then save as Yoel mentioned 

Aug 07, 2018 11:32

I tried the below code 

        public int SaveCartAsPurchaseOrder(ICart cart)
                var purchaseOrder = orderRepository.SaveAsPurchaseOrder(cart);
                var order = orderRepository.Load<IPurchaseOrder>(purchaseOrder.OrderGroupId);
                var purchaseOrderProcessor = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IPurchaseOrderProcessor>();
                return purchaseOrder.OrderGroupId;
            catch (Exception e)
                throw e;

Still it is not working. I saw the order in CM->ordermanagement->purchaseorder status shows as in-progress only.

Aug 07, 2018 12:53

Looks correct to me. Maybe just try 

order.OrderStatus = OrderStatus.OnHold;
// instead of purchaseOrderProcessor.HoldOrder(order);

But it should be the same...

Edited, Aug 07, 2018 13:02

Do you use default implementation of IPurchaseOrderProcessor?

If yes, this is how it work:

  1. Validate order status: if order status is already on hold, then no need any update, and an InvalidOperationException is throw.
  2. Set order status to On Hold, same as Joel's comment above.
  3. Validate and recalculate order.
  4. Method HoldOrder() return an instance of OrderProcessingResult, with validation issues (if any). Please check the returned object.

It should work, and AFAIK: no hidden flow changes order status to In Progress.

/Viet Anh

Aug 07, 2018 13:32

Thanks Team Order status change works,

Now I have another problem, we list the purchase order on our site for some purpose.we are using below code. After update this code not working (some old orders are retrieved but recent orders not retrieved . I can see all the orders in CM)

            searchOptions.CacheResults = false;
            searchOptions.StartingRecord = 0;
            searchOptions.RecordsToRetrieve = int.MaxValue;
            StringBuilder sqlQuery = new StringBuilder();
            sqlQuery.Append("OrderGroupId IN (SELECT oform.OrderGroupId From [OrderForm] oform ");
            sqlQuery.Append("INNER JOIN [OrderFormEx] ex ");
            sqlQuery.Append("ON oform.[OrderFormId] = ex.ObjectId ");

            if (isCounterMan)
                sqlQuery.AppendFormat("WHERE ex.{0} LIKE '{1}%' ", MetaFieldNames.WLCCustomUserGroup, WLCUserGroup.Counterman);
                sqlQuery.AppendFormat("WHERE NOT ex.{0} LIKE '{1}%' ", MetaFieldNames.WLCCustomUserGroup, WLCUserGroup.Counterman);
            sqlQuery.AppendFormat("AND ex.{0} LIKE '%{1}%'  ", MetaFieldNames.WLCCompanyPrefix, wlcPrefix);
            sqlQuery.Append("AND Status = 'OnHold')");
            parameters.SqlWhereClause = sqlQuery.ToString();
            PurchaseOrder[] purchaseOrderCollection = OrderContext.Current.FindPurchaseOrders(parameters, searchOptions);

Any sqlquery want to change please let me know.



Aug 07, 2018 17:21

Please provide how you solved the order status issue, can be good for future visitors! :)

I don't really like the OrderSearchOptions/OrderSearchParameters way of searching for orders, one of the reasons being you are free to add any SQL you want. The risk of that is the one you just stumbled upon, that the database table structure in epi is not "public api" level accessibility, i.e. they can change it without really telling people, because they mostly have APIs that cover it. They usually don't tho, but a bigger update that I think you've done, there might be some changes. 

Without knowing exactly what's changed in the database, I think it's easiest for you to just have a look at what the query is doing vs the tables it's querying against. And to find any differences there.

Aug 07, 2018 17:31

Joel Actually problem was in another side of the code. order status code works well only.

Aug 07, 2018 17:40

@karthikeyan ponnusamy 

  • Your code to create SqlWhereClause seems to be overly complicated. An if statement with String.Format should be much easier to follow. Even better if you are using C# 6.0
  • If you remove  sqlQuery.Append("AND Status = 'OnHold')"); - does it return the recent orders? It seems recent orders might not satisfy the other conditions.
Aug 07, 2018 17:52

Thanks Quan for a valuable recommendation,

I removed that "ONHOLD" too. but It does not return the orders.

Edited, Aug 09, 2018 11:12

So the problem is somewhere else, not because of OnHold status

Aug 09, 2018 11:23


Yes query making some problem.Becuase i can get all orders using below method.I will look into tables maybe it has some changes.



Aug 09, 2018 12:14

I guess there's smthing with meta fields.

Aug 09, 2018 12:23
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.