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 :
Customer experience | Sales, Customer Insights,...
Answered

How to prevent updating same entity record concurrently/simultaneously by MSCRM Plugin

(1) ShareShare
ReportReport
Posted on by 53
 We came across the issue where the current vaccine stock count was not updating/reducing correctly when two or more nurses have administered the same vaccine to different patients and marked the associated Encounter entity record of the patients completed concurrently/simultaneously using canvas application.

So the problem was when two or more nurses have administered the same vaccine to different patients same time then what we are doing here is retrieving the vaccine stock entity record which contains the current vaccine stock count field and reducing the value of current vaccine stock count field of retrieved the vaccine stock entity record by one (1) using plugin but because when two or more plugin instances run concurrently/simultaneously so each plugin execution retrieves the same value let’s say 100 for current vaccine stock count field of retrieved vaccine stock entity and then reduce the value of current vaccine stock count field of vaccine stock entity record by one (1) so the new value is 99 which is wrong as the same vaccine was administered to more than one patients and correct value should have been decremented by number of patients to whom the vaccine was administered.

Below are the steps to illustrate this:
1.  First we are retrieving the vaccine stock entity record which contains the current vaccine stock count field
2.   Then we are reading the current vaccine stock count field of retrieved vaccine stock entity record from step-1.
3.  Then we are reducing the value of current vaccine stock count field of retrieved the vaccine stock entity record by one (1).
4. Finally, we are updating the current vaccine stock count field of retrieved vaccine stock entity.
I have the same question (0)
  • Verified answer
    CRM Enthusiast Profile Picture
    53 on at
    How to prevent updating same entity record concurrently/simultaneously by MSCRM Plugin
    When two plugins are being executed and both plugins performed step 1 above (so they both retrieved same vaccine stock entity record) at same time so they both will have the same value for the current vaccine stock count field. Then they both will apply the logic in step 2 and 3.
    Finally, the first plugin will execute step 4 and update the vaccine stock entity record (this will lock the vaccine stock entity record) but it will finish execution immediately which will unlock the vaccine stock entity record. So now the second plugin is executing step 4 but since it already has retrieved the old value so the update logic will be the same and it will override the vaccine stock entity record with the same number instead of decrementing it.

    The solution for this problem lies in locking the record inside a transaction so we should update the logic in the plugin to the following:
    1.   First, do a fake update to the vaccine stock entity record so that this record is locked from update by another plugin execution.
    2.   Then we apply step 1, 2, 3 and 4 from the above logic mentioned earlier.

    All other plugins will wait for fake update to be completed first (which happens only after the later steps 1, 2, 3 and 4 from the above logic mentioned earlier are processed) by first plugin so this solves our problem of retrieving the old value of current vaccine stock count field of vaccine stock entity and updating incorrect value in same field.

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 130

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 118 Super User 2025 Season 2

#3
Erin Lubben Profile Picture

Erin Lubben 57

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans