Creating an Workflow to Update an Composite Address field

There might be certain projects where you have a custom entity with address fields, and you want have a composite field that will display the address, and be able to use that process across the board in multiple entities. The example below shows how to do that.

The first step that we need is to create the Custom Workflow Activity. You can follow the msdn article on basic instructions how to create a workflow activity, and see the sample code that is available as part of the SDK.

Inside the Custom Workflow Activity, we need to specify the input and output parameters. The input parameters will be the address fields (street 1, street2, etc...), and the output parameter will be the Composite Address.

 

        [RequiredArgument]
        [Input("Street 1")]
        public InArgument<string> Street1 { get; set; }

        [RequiredArgument]
        [Input("Street 2")]
        public InArgument<string> Street2 { get; set; }

        [Input("Street 3")]
        public InArgument<string> Street3 { get; set; }

        [RequiredArgument]
        [Input("City")]
        public InArgument<string> City { get; set; }

        [RequiredArgument]
        [Input("State")]
        public InArgument<string> State { get; set; }

        [RequiredArgument]
        [Input("Postal Code")]
        public InArgument<string> PostalCode { get; set; }

        [Output("Composite Address")]
        public OutArgument<string> Address { get; set; }

 In the Execute method of the Workflow Activity, we will get the values of the values that will be entered in the workflow, call the function to create the composite address, and then set the composite address to the Output parameter, as shown below:

        protected override void Execute(CodeActivityContext executionContext)
        {
            //Create the context
            IWorkflowContext context = executionContext.GetExtension();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            string street1 = Street1.Get<string>(executionContext);
            string street2 = Street2.Get<string>(executionContext);
            string street3 = Street3.Get<string>(executionContext);
            string city = City.Get<string>(executionContext);
            string state = State.Get<string>(executionContext);
            string postalCode = PostalCode.Get<string>(executionContext);

            string compositeAddress = GenerateFullAddress(street1, street2, street3, city, state, postalCode);
            Address.Set(executionContext, compositeAddress);
        }

 Finally, we will show the code to Generate the Composite address in the GenerateFullAddress function, as shown below:

 

        /// 
        /// Generate Full Address. Fomrat Will be:
        /// Street 1
        /// Street 2
        /// City, State, Zip
        /// 
        private string GenerateFullAddress(string line1, string line2, string line3, string city, string state, string postalcode)
        {
            StringBuilder sb = new StringBuilder();
            if (!String.IsNullOrEmpty(line1))
            {
                sb.Append(line1);
                sb.AppendLine();
            }

            if (!String.IsNullOrEmpty(line2))
                sb.AppendLine(line2);

            if (!String.IsNullOrEmpty(city))
            {
                sb.Append(city);
                if (!String.IsNullOrEmpty(state))
                {
                    sb.Append(", " + state);
                    if (!String.IsNullOrEmpty(postalcode))
                    {
                        sb.Append(" " + postalcode);
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(postalcode))
                    {
                        sb.Append(" " + postalcode);
                    }
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(state))
                {
                    sb.Append(", " + state);
                    if (!String.IsNullOrEmpty(postalcode))
                    {
                        sb.Append(" " + postalcode);
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(postalcode))
                    {
                        sb.Append(" " + postalcode);
                    }
                }
            }
            return sb.ToString();
        }

Once we have create our custom activity workflow, we need to Publish it using the Plugin Registration Tool. After we publish the workflow, we will need to create a workflow within out CRM environment. The screenshot below shows the created workflow, and the steps to run it. The image below shows the create workflow and it's steps:

Composite Address Workflow

Note that the Start options for the workflow are set to Record is created and Record fields change, so that it executes on both Create and Update. In the record fields change enter all the address fields that you want to execute the workflow on (street1, street2, city...).

Next you will see that there are two steps to the workflow.

The Generate Full Address (shown below), calls the custom workflow activity passing the address fields and gets the returned Output Composite Address field data.

AddressOnCreateUpdate

Next we will need to update the Composite Address field, by calling an Update method and passing the Composite Address that was returned from the first step, as shown in the following image:

Update Record

That's basically it. You will not be able to make changes to the address fields and have them generate the Composite Address.

The source code of this blog article is available here.