Monday, 15 August 2016

Develop a custom workflow activity in Dynamics CRM

Hi all,

In today's post I will walk through a scenario where we will develop a custom workflow activity for some requirement in Dynamics CRM.

To create a custom workflow activity (similar to plugins) we need to inherit from the abstract class "CodeActivity". The code below will show the process in which to obtain the other important properties like the organization service and the tracing service from the context.


namespace Contoso.CRM.Workflows
{
    using System;
    using System.Activities;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
     
    /// <summary>
    /// The post campaign create workflow activity
    /// </summary>
    public sealed class PostCampaignCreateActivity : CodeActivity
    {
        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {
            // Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();

            if (tracingService == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
            }

            tracingService.Trace(
                "Entered PostCampaignCreateActivity.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                executionContext.ActivityInstanceId,
                executionContext.WorkflowInstanceId);

            // Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            if (context == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
            }

            tracingService.Trace(
                "PostCampaignCreateActivity.Execute(), Correlation Id: {0}, Initiating User: {1}",
                context.CorrelationId,
                context.InitiatingUserId);

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);
           
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    var campaignEntity = (Entity)context.InputParameters["Target"];
                    if (campaignEntity != null)
                    {
                        // your logic comes here
   }
}
    }
}

The above workflow activity is for the post create of a campaign record as can be inferred from its name (the first highlight). In the second highlight I have shown how we will obtain the organization service from the service factory that we obtain from the context. Once we have these properties we can start with all the messages that the Dynamics CRM platform gives us!

Also, for those who are not aware. After registering the workflow assembly which contains all the custom workflow activity classes (like above) using the plugin registration tool (or the CRM developer toolkit), we still need to configure an out of the box workflow.
The conditions when this OOTB workflow will fire will act as the trigger for this custom workflow activity. This is required as unlike plugins we do not have steps or filtering attributes that can be specified while registering the workflow activity. Also note that images are not supported by workflows, so any requirement having to do with previous values in updates, need to be achieved through plugins.

Let me know your comments in the comments section. Happy Dynamics CRMing!!




No comments:

Post a Comment