Using Guid of Entity Record in Dynamics CRM Workflow Designer – A Generic Solution based on Custom Plugin

When we create custom Dynamics CRM workflows using the built-in Dynamics CRM workflow designer, there is a common requirement that we want to look up and reference Guid of current entity record in the workflow. For example, when a task is created or assigned in Dynamics CRM site, we often create a workflow to send the task owner an alert email with the url of the task page which consists of the guid of this task record, e.g. http://{Server} /{Organisation} /userdefined/edit.aspx?etc=4212&id={Guid}.

However, The Dynamic Value Lookup function in Dynamics CRM workflow designer does not expose the guid of an entity record. Therefore, we cannot get the guid of the current record in workflow designer.

clip_image002

A workaround for this issue is to add a custom field in the referenced entity to store the record id, and create a custom plugin to set the custom ‘RecordID’ field when a new record is created. Then we are able to get the guid of the record in Dynamics CRM workflow designer.

clip_image004

I have put together a custom plugin to set the ‘RecordID’ field. This plugin can be used for any kinds of entities in Dynamics CRM. You could find the source code of this plugin here. This post introduces how to develop the plugin and also how to deploy and register the plugin in Dynamics CRM server.

Developing the Plugin in Visual Studio 2010

Firstly, Create a Class Library project in Visual Stuido 2010.

clip_image006

Next, add references of the dependency assemblies to the project, and also sign the library.

clip_image008

Then, create a class ‘Plugin’ which implements the IPlugin intereface. Below is the source code of the Plugin class.

namespace Linxiao.CRM.Plugin.RecordIDLookupInWorkflow
{
public class Plugin: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
//Get plugin execution context service
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters["Target"] != null)
{
//Get current record
Entity record = (Entity)context.InputParameters["Target"];
//Set custom RecordID field with the Guid of current record
record["new_recordid"] = record.Id.ToString();

//Commit in the change
IOrganizationServiceFactory serviceFactory =(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service =serviceFactory.CreateOrganizationService(context.UserId);
service.Update(record);
}
else
{
//Log
}

}
catch (Exception ex)
{
//Log Exception
}
}

}
}

The code is actually very short and straightforward which just sets the custom ‘new_recordid’ field with the record id.

After you build the project, you will get the assembly of the plugin which need to be registered in Dynamics CRM server.

clip_image010

Register the Plugin in Dynamics CRM Server

The plugin developed above can be applied to any entities. This post uses ‘Task’ entity just as an example.

  1. Before we actually register the plugin, we need first create the custom ‘RecordID’ field in the ‘Task’ entity.

clip_image012

  1. Launch ‘Plug-in Registration Tool’ which is shipped with Dynamics CRM SDK (located at SDK\Tools\PluginRegistration). Then connect the registration tool to the Dynamics CRM server.

clip_image014

  1. Register a new plugin. Select the plugin assemble we just created above.

clip_image016

  1. Register a new step.

clip_image017

Set the Message as ‘Create’ and Primary Entity as ‘Task’

clip_image019

Now, the plugin has been registered in the Dynamics CRM server. You should be able to view the plugin in the ‘Customization’ area of the Dynamics CRM site.

clip_image020

You could test the plugin through adding a new task record. The ‘RecordID’ field should be automatically populated with the guid of the created task record.

clip_image022

And then you could use the ‘RecordID’ in your workflow.

clip_image023

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s