Monday, June 1, 2009

How to schedule maintenance jobs with MS CRM

Quite often a need to do regular maintenance jobs on the data in MS CRM arises. It might be that the client requests some sort of regular data processing or that customer accounts should be evaluated each month. For instance one might want to deactivate customer accounts who hasn't prchased anything the last 24 months or so. You might handle this with a filter, but it's quite nice to have the ability to automatically deactive records based on explicit criterias.

MS CRM does not support such monitoring and scheduling jobs out-of-the-box, but it is however quite easy to do this through customization

Creating the maintenance job entity
First, you need a new entity to hold information on the maintenance jobs. Create an new entity, scope it to organization, and name it "maintenance jobs". Add the following attributes:

  • A picklist attribute called jobtype - this will hold the different types of jobs you need to do. Add at least one value called "Deactivate accounts older than 24 months". This attribute is required
  • A dateTime attribute called startdate. If you want to be able to run the job at specific times, choose "date and time" - format. Required
  • A dateTime attribute called enddate. Optional
  • A picklist attribute called recurrence - assign the values daily, weekly and monthly or other values you might need. Required
  • A nText attribute called description in case you need to describe each job
  • Keep the name attribute as it is
When you're done. the new entity should look like this:












Create the self-referencing workflows
The next step is to create workflows that is triggered by this entity, and which is able to call each other recurrently. You'll need two workflows for that job. One that's triggered by the creation, and another that is started by the first one which main task is to wait until the next recurrence and trigger the first one.

Be aware that there's a continous loop detection in MS CRM which restrains workflows from triggering themselves more than 7 times per hour or so

Create the first workflow, call it WF_MaintenanceJobStarter or something like that. Scope it to organisation, make sure it triggers on create for the maintenancejob entity and that it is also available to run as a child workflow

The workflow must contain the following activities
  1. A Wait activity that waits until it's on or after staring time for the maintenance job
  2. A Check Condition activity that checks the job type of the maintenance job
  3. A Start Child Workflow activity that starts the looping activity. This isn't defined yet, so just leave this empty
  4. A check Condtion activity that checks if the maintenancejob has an end date
  5. If it has an end date a Wait activity will wait until that day and stop the workflow
This image shows the different activities that the workflow must have. Note that it calls the other workflow that isn't defined yet - you must do this in to steps. First you must define and save both workflows, then you must have them call each other



















The second workflows main task is to wait out the appropriate time established by the recurrence attribute before it calls the first workflow. You might have other recurrence patterns than those below, but remember that a workflow can't call itself more than 7 times per hour

You'll need to define the following workflow:
  • Name: WF_ MaintenanceJobLooper. Scope: Organisation, Entity: maintenance job
  • Make sure that it is available as a child workflow
  • Define a Check Condition activity with three branches, one for each type of recurrence. Dail, weekly or monthly
  • Underneath each Check Condition activity add a Wait Activity that waits a day, a week or a month after workflow execution time
  • When the waiting activity is done, a Start child Workflow Activity is fired. This activity starts WF_MaintenanceJobStarter
When you're done defining this workflow you might need to add the workflow to point 3 of WF_MaintenanceJobStarter. The finished workflow will look like this:























Now you have defined a workflow that calls it self recurrently based on the intervals you define in the maintenance job records. But it doesn't actually do anything. You need to add activities that will do the necessary maintenance for you at these intervals. Usually you'll need to code these as custom workflow activities, especially if you want to do maintenance on the entire database.

Another neat thing to remember is that when you add those activities make sure to call the looping workflow before that activity is executed. That way the maintenance job want halt if something goes wrong with your activity

You'll find a suitable custom workflow activity for this kind of job in this post.

4 comments:

  1. Rather than create the second looping workflow, what do you think about the option of adding a field 'next run date' to the custom entity, and actually triggering the workflow on Create AND Update of the maintenance job entity.

    Then you could use a single workflow to
    1) check if the 'next run date' has occurred.
    2) set the 'next run date' of the maintenance job entity to add one interval (one day, one week, etc) This would trigger the creation of a new workflow instance that will wait until that new 'next run date'
    3) execute the child workflow that actually does the work. When it completes, that workflow instance is complete, and there is already another 'waiting' workflow for that maintenance job.

    ReplyDelete
  2. Dear job seekers, most of the companies are hiring talented candidates in Gurgaon and also there are lots of Jobs in Gurgaon, So apply for those jobs immediately.

    ReplyDelete
  3. Very nice post, thanks for sharing the information. Keep up the good work.

    maintenance jobs

    ReplyDelete
  4. Nice post thank you Deborah

    ReplyDelete