I created my first state machine workflow the other day and just when I thought everything was working as expected I encountered a really strange error. I went for a state machine workflow as I had to deal with the same item being changed numerous times and send emails and create tasks depending on one of the values on the list item. From the reading I had done on state machine workflows this seemed like the perfect fit for the requirements.
I found some articles on how to create the workflow and was surprised at how easy it was, ok my workflow was very simple but I can see the advantages of this approach. This was until I started getting errors when testing the various different use cases. Before I go into the actual error its important to first give some background into the actual workflow.
When an item is added to a list the workflow is initiated and the Initial State is executed. The next state has an event driven activity and within this I have a ‘OnWorkflowItemChanged’ activity in it so I can detect when the item has been modified. I then have an if else branch with 4 possible options with different tasks in each of the branches. In the ‘OnWorkflowItemChanged’ activity I perform some calculations which are used in the branching logic.
What I was finding was everything was working as expected first time around, however if I had already created a task within a and then encountered the same situation rather than creating a new task the workflow was failing. The details in the workflow history on the list item is not helpful in the slightest and it can be difficult to locate details in the ULS log but eventually I found the error below.
System.InvalidOperationException: Correlation value on declaration "longTermSickToken" is already initialized.
Which I noticed this I looked around on Google and soon found it was because I had set the OwnerActivityName to the name of the workflow. This made sense as I guess these have to be unique so I changed the OwnerActivityName to be the name of the state but I found the same issues so next I changed it to be the name of the Event Driven Activity within my state and this corrected the problem.
I am sure people who are familiar with state machine workflows will think this obvious but it was an interesting learning curve and one I am determined not to repeat hence this article