I have written about executing recurring jobs in Dynamics 365 few times in the past. Over time, I suggested different scheduling mechanisms such as Microsoft Dynamics Workflow Timeout step or Azure Scheduler, as the pattern I suggested allows changing the scheduling mechanism without impacting other solution parts.
Flow can be also used as a scheduling mechanism, one that does not require coding like Azure Function, as it has a built in integration with Microsoft Dynamics 365 Online.
Once invoked on schedule, the executing component query Dynamics 365 for target business records and apply some business logic (Process) to each business record.
In this post I would like to demonstrate a solution for executing a recurring job in Microsoft Dynamics 365 using Flow as the scheduling component.
Sample business requirement
Weekly Leads Evaluation: once a week, disqualify all Leads that are more than 5 days old and are not rated ‘Hot’.
Add a note to each Lead record to indicate that it was disqualified by an automated process.
This Workflow Rule will be applied to each valid target business record. It simply attaches a Note and changes the Lead record state to Disqualified. It can be a/synchronous, but you may want to start with a asynchronous execution to monitor the process execution.
Make sure you define the process as an on-demand process with no automated triggers.
Using the Advanced Find, define a new query to retrieve all Lead records where Created On date is older than 5 days and are not rated ‘Hot’.
Click to Edit Columns button and remove all possible columns to maximize the query efficiency.
Click the Download Fetch XML button and open the resulting file with some text editor. Copy the FetchXML query text to the clipboard
This record is used to orchestrate by holding a FetchXML query to define the target business records and also the Action process which will be applied on the the target business records.
If you imported an unmanaged solution, find the Batch Process entity and set it to appear in the Settings area.
Create a Batch Process record and paste the FetchXML query text into the Target Records textbox.
Name the record ‘Weekly Leads Evaluation Process’.
Select the Disqualify Lead Workflow (created in step 2) in the Process Lookup field and save the record.
Copy the Reference Token value for the next step.
In your tenant, navigate to the Flow area and create an empty Flow named Weekly Leads Evaluation.
Define a Recurrence Trigger to trigger the batch process
Next, define a List records action.
In the Filter Query text box, paste the Batch Process Reference Token value and precede it with dyn_referencetoken eq (e.g. dyn_referencetoken eq ‘LOGY8Y1W6’).
Define Update record action which will automatically wrap it with Apply to each wrapper. Add a Condition before the Update Action to verify that the Flow operates only if the Batch Process record status is Scheduled.
Change the Batch Process record status to Scheduled and save.
Activate the Flow Rule.
You can monitor the Flow activation by navigating to the Run History area.
For each run, you can see the completion status (success/failure) and failure reason
Similarly, you can now add additional scheduled batch processes.