Try our conversational search powered by Generative AI!

How to clone existing order and create new order in episerver


Hi ,

we are facing problem in clone existing order and creating new order. we are getting errror in at this line.

Error:It is cloning same order id , pls help

PurchaseOrder purchaseOrder = PurchaseOrder.LoadByOrderGroupId(orderId);
Cart retVal = null;
MetaDataContext.DefaultCurrent = OrderContext.MetaDataContext;
PurchaseOrder clonedPurchaseOrder = (PurchaseOrder)purchaseOrder.Clone();
clonedPurchaseOrder.SetParent(clonedPurchaseOrder); // error in this line 

Aug 25, 2017 19:40

Why do you need to call SetParent there? And to itself? If you want to link it to the original PO, you should have called clonedPurchaseOrder.SetParent(purchaseOrder); instead

Aug 25, 2017 19:55

Tried and receiving the following error

"Cannot insert duplicate key row in object 'dbo.OrderGroup' with unique index 'IX_OrderGroup'. The duplicate key value is (86a19704-390a-4e43-abdc-d3b526a8702d).
The statement has been terminated."

Do we have any other option to copy the same order with different order id ? If so please let me know. Its urgent

Aug 25, 2017 20:11

What are you trying to do? Are you trying to create a same order? I.e. are you trying to allow customer to re-order (create a new order with same items and information)? Or are you trying to create the entirely same order (keep all the statuses, etc. so if the order is completed then the new order is compled as well)? 

Aug 25, 2017 22:47

It seems you want to create new order, so here a sample code if you are using CartHelper:

CartHelper ch = new CartHelper();


So Cart has an Add(OrderGroup) method which allow you to copy from existing PO. From there you can save it as a new PO (.SaveAsPurchaseOrder()).

Aug 25, 2017 22:50

I want to create order with existing order information and I just need to edit some details like shipment and order name. Can you send sample code for this ?

Aug 26, 2017 5:14

The code above that quan wrote will do just the thing.

CartHelper ch = new CartHelper();
var copiedPurchaseOrder = ch.Cart.SaveAsPurchaseOrder();
// Now you have a copied purchase order, modify it as you wish
Aug 26, 2017 9:02

I tried the above code and looks like the shipment is not copied in new order. Can you please send the sample syntax to copy/edit shipment ?

Aug 28, 2017 7:36

What's the value of 



Aug 28, 2017 7:49

I can see the value while debugging but the shipment is not showing up in episerver backend. Am i missing something ? Pls advice

Aug 28, 2017 7:57

Hi Uma,

Before SaveAsPurchaseOrder you may need to run CartPrepare/CartCheckout work/activity flow if you use old abstraction and simulate the checkout ... ? depending on what you want to achieve, hope it helps...


Aug 28, 2017 15:01

Can you  send sample code for CartPrepare/CartCheckout work/activity flow ? Since I am new to episerver I am not aware of this

Aug 28, 2017 16:01

Aug 28, 2017 16:16


I am facing another issue. How can i update line items which is inside shipment ? 

Please Advice

Aug 29, 2017 7:51

I can highly advice you to go on a course. Episerver Commerce is a little bit complexer than Episerver CMS, there for it is important to understand concepts and apis.

Also buy the bible "Pro Episerver Commerce", take some hours and read it, you'll get your answers there too


Aug 29, 2017 10:29
* 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.