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 

#181652 Aug 25, 2017 19:40
  • Member since: 2011

    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

    #181653 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

    #181654 Aug 25, 2017 20:11
  • Member since: 2011

    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)? 

    #181655 Aug 25, 2017 22:47
  • Member since: 2011

    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()).

    #181656 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 ?

    #181657 Aug 26, 2017 5:14
  • Member since: 2016

    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

    #181658 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 ?

    #181665 Aug 28, 2017 7:36
  • Member since: 2016

    What's the value of 



    #181666 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

    #181667 Aug 28, 2017 7:57
  • Member since: 2007

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


    #181699 Aug 28, 2017 15:01
First   1 2   Last