CRM On-line

May 10, 2011 at 1:54 AM

Hi, I'm wanting to use an auto numbering solution for CRM On-line.  You mention that the code will need to be modified for this, I assume that this is because Mutex is not available in the cloud.  Can you give any suggestion as to how we could guarantee uniqueness without using Mutex?  Thanks, Adam 

May 10, 2011 at 12:51 PM

Hi Adam,

There now a managed solution available that supports on-premise and online.

http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/0ce262ba-c2a2-4f23-95be-e8c4eba4ae7c

You can down load and test this and obtain a license key free by registering your contact details.

Regards

Trev

May 23, 2011 at 1:29 PM

To get the code working in CRM Online you simply need to replace the Mutex with a locking object.  Take a look.

        // Fields
        //private static Mutex _sync = new Mutex(false, "CRM.Plugin.SequenceGenerator.SequenceGeneratorPlugins");
        private static object _lockingObject = new object();

        // Methods
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            ITracingService service = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IOrganizationService service2 = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(new Guid?(context.UserId));
            if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"] is Entity))
            {
                Entity entity = context.InputParameters["Target"] as Entity;
                //_sync.WaitOne();
                try
                {
                    lock (_lockingObject)
                    {
                        SequenceHelper settings = SequenceHelper.GetSettings(service2, entity.LogicalName);
                        if ((settings != null) && !entity.Attributes.Contains(settings.PropertyName))
                        {
                            int next = settings.CurrentPosition + 1;
                            string str = "";
                            if ((settings.Prefix != "") && (settings.Postfix != ""))
                            {
                                throw new InvalidPluginExecutionException("The configuration is invalid. Prefix and Postfix are both provided. Please contact adminsitrator with the issue.");
                            }
                            if (settings.Prefix != "")
                            {
                                str = settings.Prefix + next.ToString();
                            }
                            else if (settings.Postfix != "")
                            {
                                str = settings.Postfix + next.ToString();
                            }
                            else
                            {
                                str = next.ToString();
                            }
                            entity.Attributes.Add(settings.PropertyName, str);
                            settings.Increment(service2, next);
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new InvalidPluginExecutionException(exception.ToString());
                }
            }
        }
    }

Thanks,
Andrew
Jun 2, 2011 at 12:01 PM

The lock only works if CRM is running on a single server. With CRM Online your CRM could be running on multiple servers, so there is a chance that duplicate IDs get generated on a busy system, e.g. when doing a data import.

Jul 6, 2011 at 1:31 PM

hey there,

I posted a solution on codeproject apparently after this question was raised.

 

http://www.codeproject.com/KB/cs/AutonumberingCRM2011.aspx