Create Custom Workflow Activities in Dynamics 365

Introduction to Custom Workflow Activities in Dynamics 365

Dynamics 365 offers robust automation through workflows. While built-in actions cover many scenarios, complex business logic often requires custom development. This is where Custom Workflow Activities come into play, allowing developers to inject C# code directly into Dynamics 365 workflows.

This article will guide you through the process of creating, registering, and utilizing a Custom Workflow Activity in Dynamics 365.

What is a Custom Workflow Activity?

A Custom Workflow Activity is a .NET class that extends the functionality of Dynamics 365 workflows. It enables the execution of custom code within a workflow process, offering capabilities such as:

Prerequisites

Before you begin, ensure you have the following:

You will need to reference the following assemblies in your project:

Step 1: Create a Class Library Project

Begin by opening Visual Studio and creating a new Class Library project using the .NET Framework. Name your project something descriptive, like CRMONCE.CustomWorkflow.

Next, add the required CRM SDK references to your project.

Step 2: Create the Workflow Activity Class

Create a new class within your project. This class will inherit from CodeActivity and define the logic for your custom workflow activity. Here's an example of a simple workflow activity that generates a greeting:


using System;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;

namespace CRMONCE.CustomWorkflow
{
    publicclassGenerateGreeting : CodeActivity
    {
        [Input("CustomerName")]
        publicInArgument<string> CustomerName { get; set; }

        [Output("GreetingMessage")]
        publicOutArgument<string> GreetingMessage { get; set; }

        protectedoverridevoid Execute(CodeActivityContext executionContext)
        {
            string customerName = CustomerName.Get(executionContext);
            GreetingMessage.Set(
                executionContext,
                "Welcome " + customerName + " to Dynamics 365!"
            );
        }
    }
}

This workflow activity takes a CustomerName as input and sets a GreetingMessage as output.

Step 3: Build the Project

Build your solution in Visual Studio. A successful build will produce a DLL file, typically named CRMONCE.CustomWorkflow.dll. This DLL contains your custom workflow activity and will be registered in Dynamics 365.

Step 4: Register the Assembly

Open the Plugin Registration Tool and connect to your Dynamics 365 environment.

Select Register New Assembly.

Choose the DLL file you just built (e.g., CRMONCE.CustomWorkflow.dll).

Configure the deployment options:

Click Register.

Step 5: Verify the Workflow Activity

After registration, navigate back to the Plugin Registration Tool. Expand the registered assembly, and you should see your custom workflow activity listed (e.g., GenerateGreeting). This confirms it's ready for use in Dynamics 365 workflows.

Step 6: Create a Workflow

In Dynamics 365, navigate to Settings > Processes.

Create a new Workflow.

Select the entity on which the workflow should run (e.g., Contact).

Add a new step to your workflow and select your custom workflow activity (e.g., Generate Greeting).

Configure the input values for your activity (e.g., set the CustomerName) and save the workflow.

Step 7: Execute the Workflow

Run the workflow manually or configure it to trigger automatically based on specific events (e.g., when a Contact record is created or updated).

Upon execution, your custom workflow activity will run, process the input, and return the output value. For our example, the output might be: Welcome John Smith to Dynamics 365!

Real-Time Example Scenario

Consider a business requirement: "When an Account is created, automatically generate a unique customer code and store it in a custom field." A Custom Workflow Activity is ideal for this:

Achieving this with standard workflow actions alone can be cumbersome or impossible.

Benefits of Custom Workflow Activities

Best Practices

Common Use Cases

Custom Workflow Activity vs. Plugin

While both Custom Workflow Activities and Plugins extend Dynamics 365, they serve slightly different purposes:

Feature Workflow Activity Plugin
Workflow Integration Yes No
Reusable in Workflows Yes No
Event-Based Execution Limited (within workflows) Yes (on various events)
Custom Logic Yes Yes
User-Friendly High (within workflow designer) Medium (requires workflow to call it)

Conclusion

Custom Workflow Activities are a powerful tool for extending Dynamics 365 workflow capabilities with custom C# code. They empower organizations to automate complex business processes, perform advanced calculations, and integrate custom logic directly within their workflows, significantly improving business efficiency and enhancing the overall functionality of Dynamics 365.

By following the structured approach outlined in this guide, you can confidently create, register, and deploy Custom Workflow Activities to meet your unique business needs.