Get bool/Two Options response from Dialog Prompt CRM 2011

Paul Nieuwelaar, 20 August 2013

In Dynamics CRM 2011, when we create a Prompt and Response of type Option Set (radio buttons or picklist) we can set the Data Type to Integer which gives us the Option Set value in the response. This value can then be used to directly update an option set field on our entity.

 Get bool Two Options response from Dialog Prompt CRM 2011

Unfortunately the same functionality is not available for Two Option fields; that is, we cannot have a prompt/response to capture a Yes/No value, and directly update a Two Options field on our entity with the response, without having to check ‘if it = yes, set it to yes… if it = no, set it to no’ etc. This can also be a problem if you have several Two Options fields you want to set at once, and we don’t want to have to do more Updates if we don’t have to.

What we can do to solve this, is write a simple custom workflow activity. The workflow activity simply takes a string input, and converts it to a bool value and returns that to our workflow for further processing.

NOTE: I’ve made it a string to allow us to input the response label or value.

A code sample for the workflow activity can be seen below. As you can see it simply takes the input, and determines whether it is true or false. Once we have our response, we can continue to update records in our dialog using the new value, and we don’t have to do any tricky checks around what the response value is.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;

namespace Test.Workflows
    public class Test : CodeActivity 
        //declare the params for input and output, and a default value if needed 
        [Input("Bool Value")]
        public InArgument<string> Value { get; set; }

        public OutArgument<bool> Result { get; set; }

        protected override void Execute(CodeActivityContext context)
            bool result = false;

            //get the input value 
            string input = Value.Get<string>(context);
            if (!string.IsNullOrWhiteSpace(input))
                //check if it's a true value (1, true, or yes), anything else will be false 
                string[] trueValues = { "1", "true", "yes" };
                result = trueValues.Contains(input.ToLower());

            //assign the output param back to the workflow 
            this.Result.Set(context, result);

 Get bool Two Options response from Dialog Prompt CRM 2011