A Workaround for Custom Workflows in CRM Online 2011 Part I

Note:  A Part II of this post is located at http://blogs.inetium.com/blogs/azimmer/archive/2011/03/13/a-workaround-for-custom-workflows-in-crm-online-2011-part-ii.aspx

Earlier this week I was at CRM 2011 Developer training.  The presenters were Rich Carr and David Yack.  The training provided me with the opportunity to really try out the new platform extensions in CRM 2011 and along the way I asked plenty of questions.
 
One question has been on my mind for a while… 

The Question: 
With the fact that CRM 2011 Online deployments do not support custom workflows, how can we add links to emails in workflows?  This was a widely-used custom workflow provided with the CRM 4.0 Business Productivity Accelerator.   David Yack’s answer was to use the plugin command pattern. 
 
The plugin command pattern uses a custom entity to trigger custom logic within a plugin.  You add fields to the custom entity for input parameters and output parameters.  You then write an out-of-the-box workflow that creates a record of this entity type and sets the input fields on the entity.  When the record is created it triggers a pre-create plugin for that entity within sandbox mode (so it can work in CRM Online).  The plugin sets the output fields and the workflow can then use those fields in later steps.
 

Step by Step Example

Start by opening your solution and creating a new entity.

Specify the basic entity information and set the entity to display in the settings area.  Be sure to uncheck all options under communication and collaboration.  This is a basic entity and it does not need these features.

Begin adding your input fields.  The first field is the hyperlink URL.  It will contain the URL along with a token where the ID field will be inserted.

Add a field to contain the hyperlink text.  This allows you to add custom text such as “Click to Open John Doe.”

Add an output field to contain the markup of the hyperlink.  This will contain the anchor tab.

The last field will include a reference to the contact.  We need this to get the ID of the current record.

Note:  Be sure to select “Do not Display” in the section for “Navigation Pane Item for Primary Entity”.  This will prevent these records from being displayed on the related contact’s form.

Now that the fields are all setup, add the fields to the form.  I have broken them out into input and output sections. 

Note:  The name field is required so I added it as well.  It isn’t used by my solution.  We could have re-used it for one of the input fields, but I did not do so.

Plugin Development

Now we need to create a plugin to set the output field.  This plugin will be setup as a pre-create plugin that runs within the sandbox (so it can run in CRM Online).

The plugin takes the two input fields (URL and Link Text) and generates the HTML markup for a hyperlink.  It takes this hyperlink and it writes it to a third field.

using System;
using System.ServiceModel;

// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Avtex.CrmOnline.Extensions
{
  public class WorkflowCommandPlugin : IPlugin
 {
        public void Execute(IServiceProvider serviceProvider)
  {
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                // Verify that the target entity represents an new_plugincommand.
                // If not, this plug-in was not registered correctly.
                if (entity.LogicalName != "new_plugincommand")
                    return;

                try
                {
                    // Extract fields from entity using proper casting
                    var entityRef = entity.Attributes["new_contactid"] as EntityReference;
                    string urlBase = entity.Attributes["new_url"].ToString();
                    string linkText = entity.Attributes["new_linktext"].ToString();

                    // Add url base and link text to hyperlink markup
                    string markup = string.Format("<a href='{0}'>{1}</a>", urlBase, linkText);

                    // Insert ID into {0} Token
                    entity.Attributes["new_hyperlinkmarkup"] = string.Format(markup, entityRef.Id.ToString("D"));

                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
                }
            }

        }
    }
}

Register the Plugin

Download the CRM 2011 SDK and run the plugin registration tool from sdktoolspluginregistration.  Connect to your CRM environment.

Register your new assembly to the database in sandbox mode.

Add a new step to your one class.  Set the step to run on create of the Plugin Command entity.  Set it as a pre-operation plugin.

 

Create the Workflow

Start by creating a workflow that fires whenever a contact is created.  Maybe you want to notify a user that a contact has been assigned to them.

Add a step to create a Plugin Command record.

  • Next, add a step to create an email.  We will use the value in the output field in this email.
  • Set the From to a user of your choice.
  • Set the To to the owner of the contact.
  • Set the subject to “New Contact Created”
  • Set the regarding to the current contact record.
  • Add text to the body and feel free to include fields from the current contact.

At the bottom of the email, add the output field from our newly created record.  This will add a hyperlink to the email.

Click Save and activate the workflow.

Testing Things Out

Since we setup the workflow to run as an on-demand plugin, lets open up a contact and kick off the workflow.  We could just as easily create a new contact to test it out.

Click “Run Workflows”.

Select the “Send Contact Email” workflow

 

Open up the workflows under this contact and open up the most recent workflow.  You will see that it has sent an email.

Open up the email and click on the hyperlink.  It should open up the contact record.

 

Take-Aways:

This solution is a work-around to custom workflows, but it does have a key disadvantage.  To get the ID of the hyperlink, the plugin method requires you to add a reference to a specific entity  type in your custom entity.  With the custom workflow, it used the current record context to determine the ID of the record.  This would require custom work for each entity type.
 
I look forward to having custom workflows added back in during a future rollup.  For now, we will survive.

Customizations:  Click Here

-Andrew

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

85,873 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>