Workflows are a valuable tool within Dynamics 365. They are powerful and easy to configure at the same time. When using workflows you may run into scenarios where you need to schedule workflows to run at a certain time or to introduce a wait time between workflow steps.
An example of this would be if we needed to initiate a workflow to notify clients when Contract Renewal Dates become current. If these dates are spread in the future from 1 day to 5 years, you will have to cover all that time by checking frequently when a Renewal Date becomes current (equals today, or one week on the past).
The first thought will be to accomplish this using any of the two OOB available options, adding Process Timeouts to trigger functionality using time or date dependent conditions or using Wait Conditions to trigger the workflow on a field change.
However, both options can be expensive on system resources. In fact, when a timeout is encountered, the workflow unloads from the memory and it is only reloaded when the timeout condition is met. Even so, each waiting workflow is going to have a row in the asynchronous operation table to keep track of it. If your Dynamics 365 ends up with a very large number of records – and as a result waiting workflows, those extra rows do take space and would negatively affect other queries against that table.
Something to remember is that timeouts can only happen up to 24 months out and if the need to restart the server or the asynchronous service arises, all the waiting system jobs possibly being lost. In this case, you will have to restart the workflows again, possibly even manually.
For similar scenarios, a recommended solution is to have an external process to query for Dynamics 365 records once per day and then kick off the workflow at that time for the returned records. The only extra overhead on Dynamics 365 will be one query to find the records.
In our example, we are using the Windows Scheduler to call a console application to execute on demand workflows. This application will initiate a connection with the instance to run a query and to fetch your Dynamics 365 data. This will return all the records that satisfy the conditions and will trigger the workflow to update these records.
1. First download and save the console application from: github.com/OliverFlint/MSCRMWorkflowRunner / and extract sample.xml (renamed config.xml)
2. To define the query criteria, run Advanced Find on your org and export FetchXML.xml. Open it using notepad and copy the code as shown below.
3. Open the config.xml file you extracted on the first step.
- Paste the code you copied from the FetchXML.xml you generated on the config.xml file. You can clean the code, deleting unneeded attributes.
- Setup connection with your Org on the config file as shown below using the CRM org URL and system user credentials.
- Setup the on demand workflow.
- Save and Close
4. You can test the console application connection settings running the App using command prompt.
5. Next step is to register the application with your Org. Open CRM SDK the URL will be something similar to: C:\CRM2015SDK\SDK\Tools\PluginRegistration
6. Register the application opening PluginRegistration.exe
7. Setup Windows Scheduler. Open windows scheduler and create a new task on the Actions area. Name the task ‘Run Workflow’.
8. Setup task properties defining user or group and to Run whether user is logged on or not.
9. Setup the triggers: Daily and the time.
10. Setup the action, adding the workflow run console application this task to run. Click OK to confirm changes.
To summarize, we created a solution that combines a workflow to update the date, the external app “Workflow runner,” and Windows Scheduler to run this app to:
- Initiate a connection with the CRM instance,
- Run a query,
- Return all the contacts that satisfy the conditions,
- Trigger the workflow to update only these records.
For more helpful Dynamics 365 tips and tricks, be sure to subscribe to our blog!
Happy Dynamics 365’ing!