Workflows – Behind the scene

By | November 30, 2010

We were recently working on designing a tool that deletes workflow job. We thought it would be easy to just filter out all job that related to a workflow by the workflow id and then delete that’s it.
But like always we were in for a surprise. Just thought putting it down would help others in resolving this mystery.
Each Workflow has a unique id just like any other entity. But if you search for workflow jobs using this id to look for all jobs related to this workflow, you would be in for a surprise. It would not return all of the jobs that you are expecting. The reason being each time you revise a workflow i.e unpublish and publish the workflow once again, it creates a new workflow entry in the tables. If you want to look for the workflow jobs related to a particular workflow then you need to look for the workflow id assigned to each of its versions.
For workflow, you would find the following definitions available in the tables.

There will be only 1 definition record in CRM for a workflow no matter how many times you revise it.

But each time you publish a workflow it will create a new activation record.

The activation workflow record will have its parent workflowid set to the workflow definition id.
The workflow definition record will store the id of the currently active version of the workflow.
If you unpublish a worklfow and publish it again, it will add another row to the table and the status would now be as follows:

Note the activeworkflowid of the Definition record now refers the Guid of the latest version of the workflow that is published.
Now if you want to look for system jobs for this workflow, you will need to search for jobs that have their workflowactivationid set to any of the workflowid listed above in the table.
Hope this helps you find your way through the maze of workflows!!!