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.
- Create a new Workflow.
- Set the Process Name to Workflow – Contact – Recursive Example.
- Set the Category to Workflow.
- Set the Entity to Contact.
- Ensure “Run this workflow in the background” is checked.
- Click Ok.
- Check the “As an on-demand process” checkbox.
- Check the “As a Child process” checkbox.
- Clear all checkboxes under the Start when:.
- Press Save.
- Add an Update Contact Step.
- Set First Name to Dean.
- Set Last Name to Dalby.
- Click Save and Close.
- Add a Wait Condition to Workflow
- Set the wait condition to timeout for 10 minutes by
- Setting minute to 10
- And selecting Duration
- Press Save and Close.
- Add a Start Child Workflow Step:
- Select Contact
- Select the current workflow that is being created.
- 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.