web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Duplicate record while running a plugin

(0) ShareShare
ReportReport
Posted on by

Hi,

I have a plugin which executes on saving of a record of a custom entity. Problem is, when multiple users tries to create record, its assigning same number to the clients. I tried locking the create method with following code 

m.WaitOne();
try
{

//Logic for plugin

}
finally
{
 m.ReleaseMutex();
}

The problem with this is, when multiple requests are made, for some of them it throws SQL timeout exception.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Inogic Profile Picture
    639 on at
    RE: Duplicate record while running a plugin

    Before creating a record of your main custom entity, send an update request to another custom entity record (dummy entity).

     

    So, Plugin will be in a Queue to update dummy entity record and then it will create the main custom entity record.

     

    For example:

    1) If two users trigger your plugin and before the creation of record both the plugin instances will go to update the dummy entity record at the same time but Dynamics 365 OOB will manage the update request in the queue. So your custom entity record creation process will also go in the queue.

    In this way, you can solve your SQL timeout exception.

    If this answered your question, please make sure to Mark this as an Answer.

    Thanks!

  • Verified answer
    Clem Profile Picture
    2,541 on at
    RE: Duplicate record while running a plugin

    Hello,

    I got the same issue in the past.

    I don't know if you are online or onpremise and if onprem, if you have more than 1 server.

    This would explain the behavior. Multiple servers can run the plugin with a different context therefore there is no "lock" or mutex working.

    I managed it by doing something explained in that blog :

    Article : www.greenbeacon.com/.../how-to-implement-robust-auto-numbering-using-transactions-in-microsoft-dynamics-crm

    sum up :

    * Create a plugin which call the custom action created

    * in the custom message (post op !):

    ** update the record to lock the db transaction

    ** retrieve the value for the autonumbering

    ** update this value with +1 and the lock

    ** send back the value using the context.Outputparameter

    Hope it helps.

    Clément

  • MdNazar Profile Picture
    on at
    RE: Duplicate record while running a plugin

    There is a code which does all the validation, gets the last inserted value from database, increments it and creates next value.

  • ashlega Profile Picture
    34,477 on at
    RE: Duplicate record while running a plugin

    Hi,

     how are you using the "create" method? Could you post that code here?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#2
Christoph Pock Profile Picture

Christoph Pock 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans