Thursday, November 4, 2010

Delete This Record! A Custom Workflow Activity

Some time ago, I created a handy bit of Workflow code that issues a “Delete” for the record in the Workflow’s context.  Sometimes I use it in Workflows that eliminate useless records (determined by conditions), other times I use it to drive dynamic record sets.  Either way, it saves a lot of time over creating and managing Bulk Deletion jobs for records that just shouldn’t exist any longer, or that should no longer exist under dynamic circumstances.

So, without further ado:

using System;
using System.Collections;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;

namespace CrmWorkflows
    /// <summary>
    /// Defines the workflow action, and places it in a container directory
    /// </summary>
    [CrmWorkflowActivity("Delete This Record", "General Utilities")]
    public class DeleteRecord : Activity
        #region Activity Members

        /// <summary>
        /// Overridden Execute() function to provide functionality to the workflow.
        /// </summary>
        /// <param name="executionContext">Execution context of the Workflow</param>
        /// <returns></returns>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
            IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
            IWorkflowContext context = contextService.Context;

            ICrmService crmService = context.CreateCrmService();

            crmService.Delete(context.PrimaryEntityName, context.PrimaryEntityId);

            return ActivityExecutionStatus.Closed;



  1. Good work Dave. Gonna try this out myself.

  2. Forgive my ignorance, but how to I go about implementing this in my CRM system? Complile to .dll and sign it, then add as a plug-in? Or??

  3. That's right, and you'll have to register it.


Unrelated comments to posts may be summarily disposed at the author's discretion.