How to create custom workflow in Dynamics 365 CE?

A requirement that can not be handled by out of box functionality is where we use custom workflows. Custom workflow is an extension of the OOB workflows that makes the workflow robust to deliver complex requirements.

Custom workflows are very similar to the plugins in terms of creating, registration, and coding style. However, there are some differences between them:

  • Custom workflow has no pipeline and steps, as compared to plugins
  • To trigger a custom workflow an OOB workflow is required
  • A custom workflow can be used for multiple OOB workflows and Actions.

A workflow is best suited in terms of reusability, quick to use and maintenance.

Step 1:
Create new project in Visual Studio > Create Class Library(.NET Framework)

Step 2:
Add required extension using nuget Solution.
1. Microsoft.CrmSdk.CoreAssemblies(V9.0.2.3)
2. Microsoft.CrmSdk.Workflow(V9.0.2.3)
3. Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool:
     Contains registration tool to register the plugin into MS CE.

Step 3:
Let us take a scenario where we need to format the “createdon” date value and store it in the “description”.
Lets code:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Runtime.Serialization;
using Microsoft.Xrm.Sdk.Query;

namespace SampleCustomWorkflow
{
    public class CustomWorkflow : CodeActivity
    {
        [RequiredArgument]
        [Input("createdOn")]
        public InArgument<DateTime> createdOn { get; set; }
        [ReferenceTarget("account")]
        public InArgument<EntityReference> accountRef { get; set; }

        [Output("Message")]
        public OutArgument<string> DateFormat { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            try
            {
                IWorkflowContext workflow = context.GetExtension<IWorkflowContext>();
                ITracingService tracingService = context.GetExtension<ITracingService>();
                IWorkflowContext wfcontext = context.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(wfcontext.UserId);

                //Get ID of Current record(This is just for refrence. Not using though)
                //Guid AccRecId = accountRef.Get<EntityReference>(context).Id;

                //Get datetime value
                var atest = createdOn.Get<DateTime>(context);
                //Set Format 
                string Date = atest.ToString("dddd, dd MMMM yyyy");
                //Set formated datetime in output argument
                this.DateFormat.Set(context, Date);

            }
            catch(Exception ex)
            {
                //do some coding 
            }
        }
    }
}

Step 4:
Create a signing certificate

Step 5:
Build solution.

Step 6:
Register the custom workflow using registration tool.

Step 7:
Create a system workflow.

Step 8:
Once system workflow is created and custom workflow is registered, add the custom workflow to system workflow.

Step 9:
Set the properties.

Step 10:
Update the record with the formatted date.

Step 11:
Save and activate the flow.

Step 12:
Validate the logic by creating a new account

If you have any concerns or issues regarding Custom Workflows, reach out to me.

Design a site like this with WordPress.com
Get started