Recently, I had a situation where the client wanted me to update the state of all records for an entity based on criteria in many related entities.

To achieve this feature, I created a c# custom workflow activity to calculate the entities’ state. Originally and unsatisfyingly, I required at least 5 workflows that triggered off different entity criteria — 1 workflow for each related entity. I decided to investigate a different and cleaner approach. What I found is Recursive Workflows.

The recursive workflows I created worked like a batch job in that it runs every 10 minutes. The frequency of the workflow execution can be configured to any duration.

Creating a Recursive Workflow

Below are instructions for creating a recursive workflow that updates a contact’s name to Dean Dalby every 10 minutes. This is a simplified real world example.

  1. Create a new Workflow.

create-a-new-workflow

  1. Set the Process Name to Workflow – Contact – Recursive Example.
  2. Set the Category to Workflow.
  3. Set the Entity to Contact.
  4. Ensure “Run this workflow in the background” is checked.
  5. Click Ok.

create-a-new-workflow-2

  1. Check the “As an on-demand process” checkbox.
  2. Check the “As a Child process” checkbox.
  3. Clear all checkboxes under the Start when:.
  4. Press Save.
  5. Add an Update Contact Step.
  6. Set First Name to Dean.
  7. Set Last Name to Dalby.
  8. Click Save and Close.

create-recursive-workflow-image

  1. Add a Wait Condition to Workflow
  2. Set the wait condition to timeout for 10 minutes by
    1. Setting minute to 10
    2. And selecting Duration

selecting-duration

  1. Press Save and Close.
  2. Add a Start Child Workflow Step:
    1. Select Contact
    2. Select the current workflow that is being created.

select-current-workflow

  1. Save and activate the workflow.

The workflow must be started manually, so go to the contact screen, click on the “Run workflow” button in the ribbon, select the Workflow – Contact – Recursive Example and press OK. Refresh the contact screen and you will see the contact’s first and last name is Dean Dalby. If you update the first and last name to your name, then in 10 minutes, it will change back to Dean Dalby.

Recursive workflows should be used sparingly, but they are a great solution for creating scheduled batch-like processes in Dynamics CRM. I find Recursive workflows are particularly useful when a number of entities need to be processed at the same time instead of individually. Additionally, recursive workflows enable the hosting of batch processes inside Dynamics CRM. This is particularly helpful with CRM Online deployments where the client may have no servers available to host a traditional .NET console app batch process.