Wednesday, October 10, 2012

Schedule Workflow in CRM

  Sometime client wants to schedule a process run in a specific interval means like we have some promotional mails and in a specific interval these mail should be sent to the customers and there will some other process (workflows) which should be triggered in a specific interval defined by the user. In CRM we don’t have a functionality to schedule the workflow to run in a specific interval but using some techniques we will be able to achieve this functionality below I have explained the way how I have achieved the functionality of scheduling workflows

I was searching for a solution in the net for scheduling workflow in crm. I have gone through different blogs and in that two I have found as useful here is that two blogs 

Scheduling recurring workflows in CRM    by  Gonzalo Ruiz 


And in my scenario the user should be able to define the interval time and also the user should be able to create their own workflows and they should be able to schedule that workflow created by them to be triggered in a specific interval. So for this what I have did is I just created a custom entity which the user can define that which workflow should be triggered and they can also specify the intervals in this. For that in my custom entity I have created a lookup filed to workflows so the user can select the workflow which they want to trigger and for defining the interval I have added an option set filed and a number field in the form. In this option I have given option like  “ Hour , Day , Week , Month “ and the numeric filed also which they can specify the interval count means “ The selected workflow should run in every 3 days  “ so this three can be defined in the interval count field.

We can create a Workflow (Process) same as specified the above blogs means like for achieving scheduling a workflow functionality  in CRM we can able to create a workflow with some custom workflow activity

Since we need to have this functionality (schedule workflow) for running different workflow so we will not able to use the system workflow functionality “trigger child workflow” instead of that we can write a custom workflow activity which will retrieve the workflow id from the entity record created and we can also write the code to trigger the same workflow in this custom workflow activity below I have given the code for triggering a workflow

ExecuteWorkflowRequest request = new ExecuteWorkflowRequest(); 
request.WorkflowId = ((EntityReference)entity["new_workflow"]).Id;
request.EntityId = new Guid("1DCDEE97-35BB-44BE-8353-58BC36592656");
ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)service.Execute(request);


Note : - The workflow which you want to trigger in a specific interval can be created against to the same custom entity(which we used for defining the workflow to be triggered and the interval) only because in this code we need to specify the entity record id also for triggering the workflow  so if you create the workflow against this entity means in this custom workflow activity we will be able to retrieve the entity id and we can use this id for triggering the workflow  

In this custom workflow activity we will also write code to calculate and return the next execution date and time and we can have a field in the same custom entity which we can update the next execution date time.

So using the wait condition we will be able to wait till the next execution date time




It will be waiting till the next execution date time and it will be triggering the workflow defined for triggering in an interval also will be getting triggered  by the custom workflow activity defined in the workflow
So for the continuation we can make the workflow to be triggered in as child workflow and in the end we will be able to trigger the child workflow using existing functionality triggering child workflow 

Otherwise 

Anyway we will calculate the next execution date time and we will update this in this entity so in this field update also if you trigger the same workflow it will be executing like a loop …in each and every flow it will be updating the date time so the workflow gets triggered continually…





* We should be very careful when we schedule a workflow it may slow down your CRM system

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.
* For a specific entity only we will be able to create this workflow  means like when you trigger the workflow in custom-workflow  activity also we need to specify a entity id so this u can retrieve from the current entity and u can make the workflow to run against the  the same entity !