How to change order status in code?

Vote:
 

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.

////try1
 var purchaseOrder = orderRepository.SaveAsPurchaseOrder(cart);
                var order = orderRepository.Load(purchaseOrder.OrderGroupId);
                OrderStatusManager.HoldOrder(order);

////try2
var purchaseOrderProcessor = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance();
                purchaseOrderProcessor.HoldOrder(order);

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

Regards,

Karthik

#195743
Edited, Aug 07, 2018 10:08
Vote:
 

Did you save the order after these operations?

#195745
Aug 07, 2018 10:28
Vote:
 

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

#195753
Aug 07, 2018 11:32
Vote:
 

I tried the below code 

        public int SaveCartAsPurchaseOrder(ICart cart)
        {
            try
            {
                var purchaseOrder = orderRepository.SaveAsPurchaseOrder(cart);
                var order = orderRepository.Load<IPurchaseOrder>(purchaseOrder.OrderGroupId);
                var purchaseOrderProcessor = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IPurchaseOrderProcessor>();
                purchaseOrderProcessor.HoldOrder(order);
                orderRepository.Save(order);
                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.

#195759
Aug 07, 2018 12:53
Vote:
 

Looks correct to me. Maybe just try 

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

But it should be the same...

#195762
Edited, Aug 07, 2018 13:02
Vote:
 

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

#195764
Aug 07, 2018 13:32
Vote:
 

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;
            searchOptions.Classes.Add("PurchaseOrder");
            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);
            }
            else
            {
                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.

Regards,

Karthik

#195788
Aug 07, 2018 17:21
Vote:
 

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.

#195789
Aug 07, 2018 17:31
Vote:
 

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

#195790
Aug 07, 2018 17:40
Vote:
 

@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.
#195791
Aug 07, 2018 17:52
Vote:
 

Thanks Quan for a valuable recommendation,

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

#195845
Edited, Aug 09, 2018 11:12
Vote:
 

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

#195846
Aug 09, 2018 11:23
Vote:
 

Quan,

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

OrderContext.Current.FindPurchaseOrdersByStatus(OrderStatus.OnHold).

 

#195848
Aug 09, 2018 12:14
Vote:
 

I guess there's smthing with meta fields.

#195849
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.