Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
CRM tips from the vault – A series which I have started on request of my blog readers who are quite new to CRM. My blog focus on niche topics which mostly require a prior in-depth understanding of Dynamics 365. However in this series I will mostly cover the topics which are quite simple, used in almost every project implementations and more importantly have withstood the test of time.
Here is my Tip 3 – How to create a scheduled Workflow in Dynamics 365 on-premise to run everyday at specified time?
Before I go ahead and start to explain, few points to remember.
- This article only considers the workflow capabilities of Dynamics 365 and does not take into account the Power Automate capability of running scheduled flows
- The article talks only about Dynamics on-premise. However the steps described here will work equally well for Dynamics 365 online
So we have this requirement where we need to run a particular workflow everyday at some specified time. You may be thinking – Why not recursive workflows?
Recursive workflows are good. But they have one problem. If more than seven instances of the workflow are detected in memory, the platform will think of it as an infinite loop situation and then will unload all the existing instances of the workflow. Not to forget the issues of async service and persistent workflows in Dynamics CRM.
So what is the other alternative. Well we can actually easily do it in combination with Bulk Delete operation. Let’s see how we can do the same. To do this I take an example here. Say we need to sync Products and Product Variants from an external site everyday at a specific time.
Step 1: Create a Dummy Entity called Workflow Trigger.
I will create few fields here.
- Name (Primary Attribute) – To store the name of the operation
- Workflow type – An option set with two values
- Product Sync
- Product Variant Sync
Also prefer to create the entity as organization owned. For the requirement mentioned here, this is fine. However based on your requirement you can add more fields to this entity.
Step 2 – Create a Bulk Record Deletion Job
Navigate to Settings –> Data Management – Bulk Record Deletion and then click on Bulk Record Deletion.
Create new Bulk Record Deletion record. In your search criteria use the Workflow Trigger Entity.
Set the scheduled time. Currently the job is scheduled to run to run everyday at 12:00 AM. You need to set the time as per your requirement.
Complete the process to create the Bulk Deletion record job.
Step 3 – Create couple of Workflow Trigger Records
I go ahead and create couple of Workflow Trigger record for each operation. One for Product sync and the other for Product variant sync
Step 4 – Create a Workflow on Delete on Workflow trigger record.
We are at the final step where we will go ahead and create a workflow. The workflow shall fire on delete of Workflow Trigger record. As you can see when the workflow trigger record of type Product Sync is deleted, I invoke an action which will internally sync the products. And then I re-create a workflow trigger of type “Product Sync”
The same I do for Product Variant Sync.
Just activate the workflow.
Now we are all set and done. So we have done a lot of steps. But how does that help our requirement. Let’s see what happens in sequence.
- At 12 AM, the Bulk delete operation runs and deletes both the Workflow trigger records
- When the workflow trigger records are deleted, the Workflow which we set up on delete of Workflow Trigger fire.
- Inside the workflow we are checking the type of Deleted record. If the Workflow Type = Product Sync then we are calling the action which internally syncs the product with Dynamics 365
- After the action is called, we re-create the Workflow trigger record. This is a very important step as when the next time the Bulk delete operation runs, it will again delete the record which we just re-created in the workflow and this cycle will continue for every day.
And voila! We have achieved our goal of setting up a scheduled workflow which shall run everyday at a specified time.
Before I end, while this is very good process, it has it’s own limitation bounded by the limitations of Bulk delete operation. You can only earn a bulk delete operation once day at a specified time. If you have a requirement say in our case to sync products every couple of hours, this methodology won’t work.
Hope you find this trick useful.
Debajit Dutta
(Microsoft MVP)
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.