locked
CRM 2011: Plug in queuing RRS feed

  • Question

  • Hi All,

    I have a plug in which creates a auto number and stores it in a custom entity. when the next time the plug in gets triggered auto number gets incremented. i was wondering in CRM how the plugins are run. is there is some queuing involved for plug ins to be run so i will have that auto number always booked. and in CRM we won't have same number for two entity.  

    regards

    im

    Thursday, October 13, 2011 9:47 AM

Answers

  • Its not pretty but if you are on-premise you can also use existing DB locking behavior to implement sequential access for auto-numbering.

    You could create a custom DB with a custom table to store 'next ids' for multiple sequences (case ID, RMA ID, etc)

    Then, in your plugin, use ADO.NET / Entity Framework / Whatever / to call a sproc whenever you need to obtain the next id for a sequence and leverage SQL behavior like:

    1. Increment the value of your current sequence with an UPDATE statement (this will lock the value of that sequence and cause other UPDATEs to block until your stored procedure has finished) As long as the first task in your sproc is to perform the update, you will make access sequential and predictable.

    2. Get the new id and return it to your plugin as the stored procedure return value

    • Marked as answer by im197 Wednesday, November 2, 2011 10:52 AM
    Tuesday, October 18, 2011 8:20 PM

All replies

  • You need to use a lock mechanism in your plugin to be sure there is no more than one instance of you rplugin that access this custom entity.

    The lock mechanism need to use a physical resource (a shared file) if use on multiple front end servers


    My blog : http://mscrmtools.blogspot.com

    All my tools for Dynamics CRM 4.0 on my dedicated site: MSCRMTools Repository

    Thursday, October 13, 2011 9:53 AM
    Moderator
  • Are you in CRM Online or OnPremise? There is absolutely no guarantee on sequencing of plugins in CRM, there is also no way to implement database locking from a CRM plugin (not with the CRM database) therefore you cannot reliably guarantee that the number is going to be unique if two plugins execute at the same time..

    If you are on-premise you can use Tanguy's suggestion to make sure only one plugin can do the operation at the same time. Otherwise you might have to implement a web service that provides the numbering and the web service would have to do some database or file locking to guarantee uniqueness.

    There are also some solutions available that do this for you: http://www.gapconsulting.co.uk/solutions/essentials/auto-number

    There are some free solutions in Codeplex but they make false assumptions and therefore cannot provide a 100% guarantee on the uniqueness of the number. Another thing to consider is that plugins might execute in sandbox, and there are multiple worker processes in sandbox so 2 plugins can execute simultaneously from different processes.


    Gonzalo | gonzaloruizcrm.blogspot.com

    Thursday, October 13, 2011 1:57 PM
    Moderator
  • Its not pretty but if you are on-premise you can also use existing DB locking behavior to implement sequential access for auto-numbering.

    You could create a custom DB with a custom table to store 'next ids' for multiple sequences (case ID, RMA ID, etc)

    Then, in your plugin, use ADO.NET / Entity Framework / Whatever / to call a sproc whenever you need to obtain the next id for a sequence and leverage SQL behavior like:

    1. Increment the value of your current sequence with an UPDATE statement (this will lock the value of that sequence and cause other UPDATEs to block until your stored procedure has finished) As long as the first task in your sproc is to perform the update, you will make access sequential and predictable.

    2. Get the new id and return it to your plugin as the stored procedure return value

    • Marked as answer by im197 Wednesday, November 2, 2011 10:52 AM
    Tuesday, October 18, 2011 8:20 PM