November Happy Hour will be moved to Thursday December 5th.

CartValidate Workflow on custom cart

Vote:
 

I am trying to copy details of the details of the default cart to other custom named draft cart that is later to be accessed. I can successfully copy the details manually, but as I initially tried to run the CartValidate workflow for calculating totals, I got an error:

[KeyNotFoundException: The given key was not present in the dictionary.]
Mediachase.Commerce.Website.Helpers.CartHelper.RunWorkflow(String name) +375

Following is an example which causes the error:

var draftName = "draft";

var currentCartHelper = new CartHelper(Cart.DefaultName);
var currentCartDecorator = new CartDecorator(currentCartHelper.Cart);
var newCartHelper = new CartHelper(draftName);
var newCartDecorator = new CartDecorator(newCartHelper.Cart);

foreach (var lineItem in currentCartHelper.GetOrderForm().LineItems)
{
    newCartHelper.GetOrderForm().LineItems.Add((LineItem)lineItem); 
}

...
Other property setups...
...

newCartHelper.Cart.RunWorkflow(OrderGroupWorkflowManager.CartValidateWorkflowName);
newCartHelper.Cart.AcceptChanges();

The execution crashes on

newCartHelper.Cart.RunWorkflow after all the workflow activities have been done successfully.

I managed to the copy all the details manually, but I'd like to if you can run the cart validation against some custom cart?

And what is the dictionary that is throwing the KeyNotFoundException?

Or what is the suggested way of doing this copying?

Any help is appreciated.

#141335
Nov 12, 2015 13:13
Vote:
 

I found the solution myself. And actually it was already on the previous post. I accidentally copied the bottom of the code snippet from other place, where similar feature was working all the time. From there I found the solution.

Previously I was running the workflows from the CartHelper. But running the workflow directly against cart fixed the issue for me. Basically I changed the RunWorkflow line from:

newCartHelper.RunWorkflow(OrderGroupWorkflowManager.CartValidateWorkflowName);

to

newCartHelper.Cart.RunWorkflow(OrderGroupWorkflowManager.CartValidateWorkflowName);

Some instructions say that the workflows should be run directly from CartHelper, so is this a bug or something else?

I'm running EPiServer Commerce version 9.0.0

#141340
Edited, Nov 12, 2015 14:36
* 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.