- Host a service that implements the scheduling functionality and then have this service call into the CRM web service to perform actions regularly.
- Leverage the CRM workflow “wait” step.
- Alter the recurrence patterns of CRM asynchronous operations (unsupported).
I will explain the second option in this post. As an example scenario I have the following requirements: “The first day of the month and email should be received with a summary of all the accounts created last month”.
I will start with the assumption that I have already created a custom workflow activity that retrieves the accounts created last month and outputs the text necessary for my email. Now I need to follow these steps to make the workflow recurring:
1. Define triggers:
Define the workflow as On-Demand and Child workflow (not automatic triggers)
2. Define process:
3. Add the Wait:
Now you need a way to tell the workflow engine to repeat the process every month. To do so, I will first insert a wait step that will tell the workflow engine to wait for 1 month:
After the step, I need a way to “restart” the workflow. Since there are no while/for loops in the workflow designer, we must find another solution. I will simply create a recurrence pattern by having an “Execute child workflow” step which executes itself. Because the workflow is defined as a child workflow, it will call itself every month. After I activate the workflow it looks like this:
4. Trigger the workflow once
The first time that the workflow executes, it must be triggered manually, after that it will schedule itself monthly from the time that it was executed manually. Since it is defined as an on-demand workflow, you can simply navigate to your primary entity and trigger the workflow manually.
Once your workflow has been triggered for the first time, it should appear in the system jobs grid, waiting for the next month to resume execution:
Note that “month” is not the same as 30 days, it actually uses the calendar month definition, since CRM workflow uses the .NET DateTime class. Therefore, if it was first triggered the 1st of the month, it will always trigger the 1st of the month regardless of the number of days in a month.
- Every time you create a recurring workflow, you should carefully consider what resource consumption implications it will have. Recurring operations tend to utilize a large amount of resources which can cause lagging to the CRM Asynchronous processing service.
- You must select a primary entity for your workflow which will never be deleted, otherwise the entire recurring workflow will be cancelled. For example, you can set the primary entity of your workflow to be systemuser and trigger it the first time regarding your own system user.
- There is a loop detection mechanism in CRM which would cancel processes/plugins that create infinite loops as the one above. The maximum depth for a recurrence/loop is 8; however, the depth is reset after 1 hour of inactivity (wait). Therefore, the workflow above should work fine because it has 1 month of inactivity so each time it executes the depth will be reset and it will never be cancelled by the loop detection mechanism.