The curse of the deleted workflows
When you’re following a development process
including at least a development and production environment, then you might end
up in a scenario where you have deleted a workflow from the production
environment and then want to import a solution containing the very same
workflow.
In some scenario’s you will find that this
results in the error message:
0x80041103: Workflow' entity doesn't contain attribute with Name = 'Workflowid'.
Though the error message isn’t valid at all, the workflow entity definitely has the attribute Workflowid, there is an explanation for the error.
What happens when you enable a workflow, is that a copy of the workflow is created in the database, called a ‘Workflow activation’. It is this copy of the workflow that is being used when a workflow is executed. This is useful for when the workflow is being changed, the current workflows will then continue running against this copy of the workflow. Michael B. Scott describes this pretty well in a response on this forum: https://social.microsoft.com/Forums/en-US/b717ae4a-1e5d-48d2-880e-c961ff9a776d/crm-2011-workflows-being-duplicated?forum=crm
He also describes that it takes a while (max 24 hours), before the workflow activation records get deleted when they’re not used anymore. And it is exactly the existence of this workflow activation that causes the error message. This means that in a happy scenario the error should magically disappear within 24 hours.
But, sometime it doesn’t.
In that case there are still references to the workflow activation record. You can find these when searching for system jobs carrying the same name as the name of the workflow. Most likely these are the instances of the workflow that didn’t end with a success state, such as cancelled or failed workflow executions. After deleting these, then another 24 hours later, the workflow activation record should get removed.
But, how to delete these? You are lucky if this works through the CRM UI, but this can throw error messages as well as the related workflow doesn't exist anymore. In that case a bulk delete can give you the answer. Note: I would always strongly favor the CRM UI approach as this handles as much cleaning as possible for you.
But if that doesnt'work and you do have access to the database, then I have some SQL queries that can help identify the records or even clean them server-side.
-- select all records having a non existing parent (most likely
only workflow activation records)
Select name, workflowid, parentworkflowid, * from Workflowbase wwhere parentworkflowid not in (select workflowid from Workflowbase)
order by 1
-- select all system jobs related to the selected workflow
activation records
select asyncoperationid,
WorkflowActivationId, WorkflowActivationIdName, CorrelationId, statecode, CreatedOn, completedon, regardingobjectid, regardingobjectidname, regardingobjecttypecode
from AsyncOperation
where WorkflowActivationId IN
(Select workflowid from Workflow w
where parentworkflowid not in (select workflowid from Workflow))
order by workflowactivationid, CompletedOn
-- find out how many workflowlog records (basically steps in a
workflow) are related to the workflow instances
select count(*) from workflowlogwhere AsyncOperationId IN
(select asyncoperationid from AsyncOperation
where WorkflowActivationId IN
(Select workflowid from Workflow w
where parentworkflowid not in (select workflowid from Workflow)
)
)
Enjoy