Sunday, 1 August 2021

Creating Custom Workflow Activity in Dynamics 365

 In Dynamics 365 Workflows, if some functionality is not available in out of the box features, we have the option to write Custom Workflow Activity and consume that in the workflow processes. Let's see how to create Custom Workflow Activities for Dynamics 365. Here, for example, we will write an activity which will take the text as input and return work count as output.

Step 1 - Create New Project

In Visual Studio create a new project of type Class Library & select framework version 4.5.2. This might change for future versions. I have given the name as WordCountWorkflow which tells the purpose of the workflow.

Dynamics 365

Step 2 - Add Required Packages

Go to "Manage NuGet Packages" and install Microsoft.CrmSdk.CoreAssemblies (for Microsoft.Xrm.Sdk namespace) & Microsoft.CrmSdk.Workflow(for Microsoft.Xrm.Sdk.Workflow namespace).

Dynamics 365

 

Step 3 - Create WordCount Class

Create class WordCount and inherited from CodeActivity, it will require you to add System.Activities namespace.

Step 4 - Add Input/Output Parameters

Our workflow activity will require one input parameter of type text and one output parameter of type number to return the word count. Let's add them to the class. It would require Microsoft.Xrm.Sdk.Workflow to be added.

  1. using System.Activities;  
  2.   
  3. namespace WordCountWorkflow  
  4. {  
  5.     public class WordCount : CodeActivity  
  6.     {  
  7.         protected override void Execute(CodeActivityContext context)  
  8.         {  
  9.   
  10.         }  
  11.     }  
  12. }   
Step 4 - Add Input/Output Parameters 2

Our workflow activity will require one input parameter of type text and one output parameter of type number to return word count. Let's add them to the class, it would require Microsoft.Xrm.Sdk.Workflow to be added.

  1. using Microsoft.Xrm.Sdk.Workflow;  
  2. using System.Activities;  
  3.   
  4. namespace WordCountWorkflow  
  5. {  
  6.     public class WordCount : CodeActivity  
  7.     {  
  8.         [RequiredArgument]  
  9.         [Input("Input Text")]  
  10.         public InArgument<string> InputText { getset; }  
  11.   
  12.         [Output("Word Count")]  
  13.         public OutArgument<int> CountOfWords { getset; }  
  14.   
  15.         protected override void Execute(CodeActivityContext context)  
  16.         {  
  17.   
  18.         }  
  19.     }  
  20. }   
Step 5 - Add Word Count Logic & Get/Set Parameters

Inside Execute method, we are getting InputText parameter value and applying Split() to split into words then .Lengthproperty to get the count of words. Finally, we are setting this value to CountOfWords parameter.

  1. using Microsoft.Xrm.Sdk.Workflow;  
  2. using System;  
  3. using System.Activities;  
  4.   
  5. namespace WordCountWorkflow  
  6. {  
  7.     public class WordCount : CodeActivity  
  8.     {  
  9.         [RequiredArgument]  
  10.         [Input("Input Text")]  
  11.         public InArgument<string> InputText { getset; }  
  12.   
  13.         [Output("Word Count")]  
  14.         public OutArgument<int> CountOfWords { getset; }  
  15.   
  16.         protected override void Execute(CodeActivityContext context)  
  17.         {  
  18.             this.CountOfWords.Set(  
  19.                 context,  
  20.                 this.InputText.Get<string>(context).Split(  
  21.                     new char[] { ' ''\r''\n' },  
  22.                     StringSplitOptions.RemoveEmptyEntries).Length);  
  23.         }  
  24.     }  
  25. }   
Step 6 - Signing the Assembly

In Dynamics 365, it is necessary to sign the assembly before registering. To do this -

  1. Right click on project, click on properties to open.
  2. On the left pane, click on Signing.
  3. Check Sign the assembly checkbox.
  4. In Choose a strong name key file dropdown click New...
  5. Crete Strong Name Key popup will appear.
  6. Give some name.
  7. Optionally, you can protect this key file with a password.
  8. Click OK to generate the key and sign the assembly.
  9. Build the solution.

Dynamics 365

Step 7 - Register the Assembly in Dynamics 365

Open the Plugin Registration Tool and connect with your organization. If you don't already have it, grab it by adding Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool NuGet Package.

Click on Register >> Register New Assembly.

Dynamics 365

 

The "Register New Assembly" popup will appear; select your project DLL from bin/debug folder of the project.

Dynamics 365

After selecting DLL, make sure that Select All is selected in Step 2.

Dynamics 365

 

Leave the rest of the options as they are and click Register Selected Plugins. It should register your assembly successfully.

Dynamics 365

 

You can verify the assembly after registering in Plugin Registration Tool.

Dynamics 365

 

Step 8 - Consuming Custom Workflow Activity in Workflow Process

Open CRM, go to a solution where you want to create Workflow, navigate to Processes & create new. Give some meaningful name ( I've given "Word Counter"), select Category as "Workflow" & select "Contact" in Entity. I have created as a realtime workflow by unchecking "Run this workflow in the background (recommended)" checkbox, for the ease of testing.

Dynamics 365

After Creation, check "As an on-demand process" to make as On-demand workflow.

Dynamics 365

 

Click on "Add Step" and look for our assembly name i.e WordCountWorkflow, click on this WordCountWorkflow.WordCount will appear, click on this to add step.

Dynamics 365

 

In added step click "Set Properties", In properties set Input Text (Input parameter given in code) as Address field of the Contact record.

Dynamics 365

 

Add another update step, and set Job Title field as Word Count (Output Parameter given in code).

Dynamics 365

Workflow is completed; save and activate it.

Dynamics 365

 

Testing the Workflow

To test this, I have created one contact record with Address field populated which has nine words in total. Expand the top menu in form and click "Run Workflow".

Dynamics 365

 

In the popup that appears, select our "Word Counter" workflow and click "Add".

Dynamics 365

 

It will ask for confirmation to run, click "OK".

Dynamics 365

 

Let the execution complete.

Dynamics 365

 

Refresh the form to update and verify whether Job Title field is updated with the Word Count of Address field.

Dynamics 365

 

Congrats! Your Custom Workflow Activity is running successfully.

Grab the complete source code here https://github.com/AshV/Custom-Workflows-Dynamics-365.

No comments:

Post a Comment

Power Apps Drag & Drop Kanban code sample

  Introduction: The Kanban Board App Developers at a software company use the Kanban board to show their progress on development tasks and b...