Recurring CRM Plugin Execution using Workflows

Recurring CRM Plugin Execution using Workflows

Many times we are required to have a process or task that will automatically execute at a predefined interval (such as every first of the month), but Microsoft Dynamics CRM does not have a built-in engine to have such a process. An example of this would be updating the Month-To-Date totals of a particular entity.

The example below will show how to use a Workflow Process with the combination of a Plugin to execute on a monthly basis.

Many times we are required to have a process or task that will automatically execute at a predefined interval (such as every first of the month), but Microsoft Dynamics CRM does not have a built-in engine to have such a process. An example of this would be updating the Month-To-Date totals of a particular entity.

The example below will show how to use a Workflow Process with the combination of a Plugin to execute on a monthly basis.

In order to implement the example, we will start by creating a custom entity that will store the execution log of the process.
For this example we created an entity called Process Log which the required field called title.

The next part will be created a Workflow that will run on a monthly basis to create a new record in this Process Log Entity that we created.

Notice that the workflow is Available to Run manually (As an on-demand process) and “As a child process“, for the subsequent runs which will be automatic.

Next, we will require to add the steps of the Workflow. These steps will include

  • Create a Process Log Record
  • Set Timeout of Process for One Month
  • Run the Process Again as a Child Workflow Process

Create a Process Log Record

We will create a New Process Log Record with a Proper Title to distinguish between the different process Logs.
We will later create a Process Log Create Plugin Message which will execute when the Workflow creates this record.

Add a Timeout for the Process

We will need to add a timeout to the workflow engine, so that the process will repeat every month.
In order to do so we will insert a Wait Step that will tell the Workflow Engine to wait for 1 month.

After we add the Timeout for the Process we will have to call the Workflow again, so that the same process will re-execute.
We do this recurring pattern by adding a Child Workflow step that will execute wait the Timeout has been completed.

Start Child Workflow

We will use the Add Step dropdown and choose the Start Child Workflow option.
We will choose Process Log as the Entity for the Workflow and Monthly System Update as the actual Workflow.

Before we execute the initial workflow for the first time, we have to make sure that the plug-in is in place, so we will develop a Create Message Plugin.

Build Create Message Plugin

public class ProcessLogOnCreate : IPlugin
{
IOrganizationService service;

public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
Entity entity;

if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)
{
entity = (Entity)context.InputParameters[“Target”];
if (entity.LogicalName != “xrm_processlog”)
{
return;
}
}
else
{
return;
}

try
{
if (context.Depth > 1)
return;
else
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
service = serviceFactory.GetOrganizationService(context.UserId);

Guid id = (Guid)context.PrimaryEntityId;
ExecuteTasks(id);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(“An error occured in the plug-in”, ex);
}
}
}

Build, Deploy and Test the Plug-in in your CRM environment. Once we are satisfied that the Plugin works we initialize the Workflow.

Run the Workflow for the First Time

The Workflow must be instantiated manually for the first time. After the first time it is executed, it will automatically schedule itself to run monthly from the time of the original execution.
You can do this by going to the Primary Log Entity, for which the workflow was created, and Run the Workflow Manually.

After the initial run of the Workflow, you can check the Progress of the Workflow in the System Jobs, which will show it is waiting for execution to resume in one month.

Note: You should consider resources consumption when creating recurring workflows. Recurring workflows tend to use a large amount of resources in the CRM Asynchronous Processing Service.