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.
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.
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.
Next, add references of the dependency assemblies to the project, and also sign the library.
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.
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.
- Before we actually register the plugin, we need first create the custom ‘RecordID’ field in the ‘Task’ entity.
- 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.
- Register a new plugin. Select the plugin assemble we just created above.
- Register a new step.
Set the Message as ‘Create’ and Primary Entity as ‘Task’
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.
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.
And then you could use the ‘RecordID’ in your workflow.
You must be logged in to post a comment.