Monday, October 31, 2011

MSCrm 2011 - Workflow Assembly Get Current Entity ID


MSCrm 2011 - Workflow Assembly Get Current Entity ID

This is a workflow assembly that returns the current record ID (Guid) in Dynamics crm 2011.
There is only one drawback, It won't work in Dynamics CRM 2011 ONLINE because workflow assemblies aren't supported there.

Code:

    public class GetEntityId : CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            //Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();

            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            EntityId.Set(executionContext, context.PrimaryEntityId.ToString());
        }

        [Output("Entity Id")]
        public OutArgument<string> EntityId { get; set; }
    }


Walkthrough:
1) Copy the .dll from the bin folder to <%CRM folder%>\Server\bin\assembly
2) Use the plugin registration tool to register the workflow assembly
3) Now you should be able to see it in the Workflow window:


4) After you added it to the Workflow you can use it:



Download Link: https://docs.google.com/open?id=0B8k6R6QcCN7INjU2ZjIwMGQtYTBjYy00MmQ1LWI1ZWQtODg0OWViYmQ3YzRl


Dynamics CRM 2011 ONLINE users walkaround:
Create plugin on post create step insert the id to some text field.
The plugin has advantage, as it will work on all environments

Plugin version: http://dynamicslollipops.blogspot.com/2011/11/mscrm-2011-get-guid-plugin.html

3 comments:

  1. Thanks for this.

    I badly need to get the GUID of a newly created record in CRM 2011 online and expose this attribute to workflow. In my scenario, it has to occur without JavaScript.

    I have no idea how to 'create plugin on post create step'...can you point me in the right direction?

    ReplyDelete
  2. This is the plugin code, hope you'll know to compile it and create plugin out of it.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xrm.Sdk;

    namespace MSCRM2011SetGuidPlugin
    {
    public class MSCRM2011SetGuidPlugin : IPlugin
    {
    public readonly string rAttributeSchemaName = "new_guid";
    public string m_SecureConfig { get; set; }
    public string m_Config { get; set; }

    public MSCRM2011SetGuidPlugin(string i_Config, string i_SecureConfig)
    {
    m_Config = i_Config;
    m_SecureConfig = i_SecureConfig;
    }


    ///
    /// A plugin that creates a follow-up task activity when a new account is created.
    ///
    /// Register this plug-in on the Create message, account entity,
    /// and asynchronous mode.
    ///
    public void Execute(IServiceProvider serviceProvider)
    {
    //Extract the tracing service for use in debugging sandboxed plug-ins.
    ITracingService tracingService =
    (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    // Obtain the execution context from the service provider.
    IPluginExecutionContext context = (IPluginExecutionContext)
    serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    tracingService.Trace("Config: " + m_Config);
    string attr = rAttributeSchemaName;
    if (!String.IsNullOrEmpty(m_Config))
    {
    attr = m_Config;
    }
    tracingService.Trace("attr: " + attr);
    tracingService.Trace("id: " + context.PrimaryEntityId);
    Guid id = context.PrimaryEntityId;

    Entity entity = new Entity(context.PrimaryEntityName);
    entity.Id = id;
    entity.Attributes.Add(attr, id.ToString());
    service.Update(entity);
    }
    }

    }

    ReplyDelete
  3. http://dynamicslollipops.blogspot.com/2011/11/mscrm-2011-get-guid-plugin.html

    ReplyDelete