The workflow feature of CRM is a flexible GUI tool which allows users to create complex business processes without any knowledge of programming. CRM provides many workflow activities (which are essentially steps in a workflow) out of the box to accommodate most business process needs. These CRM custom workflow activities provide if…then conditions, wait conditions, record manipulation activities, and email and task activities.
If there is need for a workflow activity which CRM does not provide, a .NET developer can write a CRM custom workflow activity by inheriting the CodeActivity class provided in the System.Activities namespace. CodeActivity provides the abstract method Execute to implement the logic for the custom workflow activity.
Let’s examine a simple CRM custom workflow activity to calculate a business date.
First, we create a class library project in Visual Studio 2010. It is best practice to suffix a custom workflow activity project with “WorkflowActivity,” so we’ll name this project “AddBusinessDaysWorkflowActivity.” Add references to the Microsoft.Xrm.Sdk and Microsoft.Xrm.Sdk.Workflow assemblies provided by the CRM 2011 SDK.
A custom workflow activity assembly must be signed in order for CRM to allow the assembly to be registered. Use the Signing tab of Project Properties to sign the assembly.
Rename Class1.cs to AddBusinessDaysWorkflowActivity.cs. Open this file and add the following using statements:
Scope the class as public, and inherit from CodeActivity:
We need three properties to calculate a business date. An input property to specify the target date, another input property to specify the number of business days to add to the target date, and an output parameter to return the result.
Note the use of the Input and Output attributes provided by the Microsoft.Xrm.Sdk.Workflow namespace.
Next, we implement the Execute override to provide the logic of calculating a business date. We’ll get the values for target date and number of business days to add, and perform that calculation taking into account business days exclude Saturday and Sunday. Once we have our result, the Result property will be set.
Now, we have our CRM custom workflow activity. We build the project and register the assembly with CRM using the Plugin Registration Tool. Once that is complete, we can see the custom workflow activity is available to be used in the workflow designer:
Let’s design a workflow to test our Add Business Days activity. The workflow will be configured to fire on the creation of a Task record and will set the Due Date to 10 business days after the Created On date. We need two steps: one which utilizes the Add Business Days custom workflow activity to calculate the Due Date, and another which sets the Due Date property on the Task with our calculated value. The steps would look like this in the CRM workflow designer:
When we click the Set Properties button for the “Calculate due date” step, CRM presents us with a dialog to set the workflow activity’s input properties. These properties correspond to the Target Date and Business Days to Add properties we marked as Input properties in the code. In this scenario, we want the Target Date to be the Created On date for the Task, and 10 for the Business Days to Add property.
When we set the properties for the “Set due date on task” step, we assign the Result property of the above step as our value for the task’s Due Date. If you recall, Result was the property we marked as Output for the custom workflow activity.
If we save and activate this workflow, we can create a task record to check our work. You will see that the Due Date gets calculated as 10 business days after the Created On date.
From the example above, you can see how powerful CRM custom workflow activities can be. Although the out-of-box workflow activities provided within CRM can be very useful, you may find yourself in a situation where you need a workflow activity that does not already exist. In these situations, a .NET developer can come in and write custom workflow activities to behave the way your business requires. Tribridge is staffed with many highly experienced .NET developers who can make customizations within your CRM to help it perform to your organization’s specifications.
Don’t forget to check back our blog soon. In my next post, I will address examining the query techniques of CRM.