Example Activities: Math Building Blocks

When designing custom workflow activities, you will find that they tend to fall into one of two categories. Either they are generic building blocks that can be used by a variety of workflows, or they are extremely specialized and only suited to the task they were originally designed for. Whenever possible, try to design your custom activities to be reusable across workflows. This may mean that you end up implementing multiple smaller activities, but the users that take advantage of your activities will thank you for the flexibility.

In this example, we will demonstrate how a series of simple activities can provide a more flexible workflow design environment. Example 6-10 shows the full definition of the AddValuesActivity.

Example 6-10. AddValuesActivity source

using System.Workflow.ComponentModel;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Workflow;

namespace ProgrammingWithDynamicsCrm4.Workflow
{
    [CrmWorkflowActivity("Add Values", "Math")]
    public class AddValuesActivity: Activity
    {
        public static DependencyProperty ValueAProperty =
           DependencyProperty.Register(
           "ValueA", typeof(CrmFloat), typeof(AddValuesActivity));

        [CrmInput("Value A")]
        [CrmDefault("0")]
        public CrmFloat ValueA
        {
            get { return (CrmFloat)GetValue(ValueAProperty); }
            set { SetValue(ValueAProperty, value); }
        }

        public static DependencyProperty ValueBProperty =
            DependencyProperty.Register(
            "ValueB", typeof(CrmFloat), typeof(AddValuesActivity));

        [CrmInput("Value B")]
        [CrmDefault("0")]

        public CrmFloat ValueB
        {
            get { return (CrmFloat)GetValue(ValueBProperty); }
            set { SetValue(ValueBProperty, value); }
        }

        public static DependencyProperty ResultProperty =
            DependencyProperty.Register(
            "Result", typeof(CrmFloat), typeof(AddValuesActivity));

        [CrmOutput("Result")]
        public CrmFloat Result
        {
            get { return (CrmFloat)GetValue(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(
            ActivityExecutionContext executionContext)
        {
            double a = 0;

            if (this.ValueA != null)
            {
                a = this.ValueA.Value;
            }

            double b = 0;
            if (this.ValueB != null)
            {
                b = this.ValueB.Value;
            }

            this.Result = new CrmFloat(a + b);

            return ActivityExecutionStatus.Closed;
        }
    }
}

This sample uses techniques discussed throughout the chapter to build a simple activity that can be used to add two numbers together and expose the result through an output property. Even though this example uses CrmFloat as the input and output properties, the native workflow designer is smart enough to convert other numerical types such as integers and currency into and out of the CrmFloat correctly.

Using this example, you could easily build a series of activities to perform various mathematical operations. At that point, you could configure a workflow to pass the results of one math activity in as the input values to another math activity. By chaining these activities together, you can perform complex accounting formulas to update an attribute on an entity. By keeping the scope of your custom workflow activities simple, you can provide the users that design workflows with a powerful toolset.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset