Just do it – Deleting attributes that are in use by running Workflows

Microsoft Dynamics CRM MVP Aaron Elder of Ascentium provides some tips and tricks.

Recently, we had the need to delete several attributes from an entity we had created (Employee).  Normally this is a very straight forward affair in Microsoft CRM.  You simply remove the field from the Form and Grids and they using the Entity Editor select the attribute and hit “delete”.  In this case, the attributes in question, were being used by workflow.  Unfortunately, the error you get from CRM is not terribly helpful:

 WF - Delete Error

While the steps to take are indeed correct and actually rather useful, the error does nothing to tell you which Workflow is causing the problem.  This was especially a problem for us because we happened to have almost 20 workflows running on the Employee entity and unpublishing and hunting through each of these would have been a real pain.  Of course with a little bit of SQL, there is a better way:

   1: -- Run this against your Org_MSCRM database 
   2: DECLARE @EntityYouAreTryingToModify VARCHAR(50) 
   3: DECLARE @AttributeYouAreTryingToDelete VARCHAR(50) 
   4: -- TODO: Update these to the entity and attribute you are working with 
   5: SET @EntityYouAreTryingToModify = 'invoke_employees' 
   6: SET @EntityYouAreTryingToModify = 'invoke_hardwaremacintoshdesktop' 
   7: SELECT    WFD.WorkflowDependencyId AS 'workflowdependencyid', 
   8:         WF.Name FROM WorkflowDependency AS WFD 
   9:     -- Join out to the Workflow Table to get the Name of the Workflow 
  10:     JOIN Workflow AS WF ON 
  11:         WFD.WorkflowId = WF.WorkflowId AND 
  12:         WF.StateCode = 1 
  13:     WHERE 
  14:         WFD.DeletionStateCode = 0 AND 
  15:         WFD.DependentEntityName = @EntityYouAreTryingToModify AND 
  16:         WFD.DependentAttributeName = @EntityYouAreTryingToModify AND 
  17:         WFD.Type = 8 AND 
  18:         WF.DeletionStateCode = 0

This query, will return a list of the Workflows that are currently published and that have a dependency to the field you are trying to delete.

Now that you know what Workflows are causing the problem, the steps to fix this are pretty simple:

1. Unpublish the Workflow(s)

2. Remove the dependency (In the “Properties” of a Step, the “Record Attributes Change” list, etc)

3. Re-publish your Workflow

4. Delete your attributes

Now, if you happen to instead:

1. Unpublish the Workflow(s)

2. Delete the Attribute

3. And try to re-publish the Workflow(s)

 WF - PublishError

You are going to run into a “Generic” error when you try to re-publish the Workflow.  This is because the CRM delete attribute process is smart enough to delete the dependencies from part of the Workflow, but not all.  If you find yourself in this situation, don’t worry… just open the Workflow editor using the User Interface, remove the dependency and “re-save” it.

WF - Editor

The Workflow UI XML and various other XMLs will get “re-updated” with the dependency removed.  You can now re-publish the workflow and all should be well.


Aaron Elder

This posting is provided "AS IS" with no warranties, and confers no rights.

Comments (2)

  1. Wim Co says:

    Another nice post by the guys @ Ascentium! Carefull if you would copy-paste the code however, there seems to be a small (find-and-replace?) error in it.

    You probably all saw it, but I think line 6 should be:

    6: SET @AttributeYouAreTryingToDelete = ‘invoke_hardwaremacintoshdesktop’

    and line 16:

    16: WFD.DependentAttributeName = @AttributeYouAreTryingToDelete AND

  2. Tim Long says:

    I think you’ve highlighted an area that is generally weak in the CRM product. The error messages are almost universally unhelpful and, as a rule, don’t actually tell you what the actual problem is. Error messages need to give enough information for the user to take some intelligent remedial action or make an appropriate decision. An error message that says "An error has occurred" and gives no useful information is a shameful situation. I hope to see improvements in this area in CRM V.next