SBX - Search With Button

SBX - Forum Post Title

Force Optimistic Concurrency using plugin

Microsoft Dynamics CRM Forum

Federico Jousset asked a question on 12 Dec 2016 11:45 AM

Question Status

Suggested Answer

Hi all,

I'm trying to enforce the optimistic concurrency control for changes done via UI. To accomplish this I've created a plugin which is triggered in the Update message (Pre-operation stage) and sets the ConcurrencyBehavior setting to IfRowVersionMatches.

In theory, this should work smoothly but it actually does not. After some tests I've found that the RowVersion property is always null:


Without the RowVersion, CRM can't compare the entity being updated with the current version of the record so the validation is never triggered.

I know that this feature is something relatively new (MSND states "Optimistic concurrency behavior can only be set through an SDK call. There is presently no setting for it in a form of the Web application.") but I would expect that at least it should be possible to access the RowVersion from the plugin context and force this behaviour.

I've read about some issues related the RowVersion in the context of a (sandboxed) plugin so I'd like to know if this is a known problem or possible workarounds.

Thanks!

Reply
Pawel Gradecki responded on 12 Dec 2016 5:15 PM
My Badges
Suggested Answer

Hi,

According to the documentation that you provided you have to retrieve the entity. You are not retrieving entity, you are using "Target" object in a plugin, which is not the same (target is passed to the plugin in the context and does not contain all the information that resides in the entity). Try to Retrieve this entity in this plugin - RowVersion should be filled.

Reply
Alagu nellaikumar.S responded on 12 Dec 2016 11:46 PM
My Badges
Suggested Answer

Hi

Why don't you try post image?

Reply
Federico Jousset responded on 13 Dec 2016 4:12 AM

Hi Pawel, I'm trying to force the optimistic concurrency control for updates being done via User Interface so it does not make sense to retrieve the entity in the plugin (the RowVersion could be different). Actually, the Retrieve of the entity was performed by CRM when it loaded the form, so it doesn't seem too crazy to assume that the RowVersion attribute should be there.

Reply
Pawel Gradecki responded on 13 Dec 2016 6:10 AM
My Badges

I can't see the relation here - ok CRM retrieves the entity on the form, but it does not pass this to the target. In target you have only fields you changed and some mandatory fields so it has no relationship with the entity that was retrieved for the form. If this would be so easy there would not be the statement that this is not working on the form (in the link you provided). I can't see any nice answer here, looks like this is not going to work in this scenario, maybe somebody else has some more ideas.

Reply
Federico Jousset responded on 13 Dec 2016 11:16 AM

Hi again Pawel,


Yes, I know that probably there's no nice answer/solution and that's why I was asking if this a know issue or a limitation. In previous releases there were some bugs related to the RowVersion from plugin context  (even using the Retrieve method) and it seemed possible that some of those issues are still there. Basically the problem is the one that you described, the RowVersion is not being passed to the Target and in my opinion that's something that should happen (it should be one of those "mandatory fields" of your message, it's not an attribute). Maybe it's just a matter of waiting for this feature to be fully implemented...

In the meantime, I've found that it's possible to access the RowVersion from the client side using the following:

Xrm.Page.data.entity.getRowVersion()

I'll play a little bit to see if it's possible to implement a workaround using this.

Thanks!

Reply
Pawel Gradecki responded on 14 Dec 2016 3:59 AM
My Badges

Hi,

Please let us know how it went, this may be some interesting feature

Reply
Patrick V responded on 15 Dec 2016 2:13 PM
Suggested Answer

I'm seeing this in CRM 2016 on-prem as well.  I have a plugin that generates a custom ID for an entity.  I have a non-sandbox, synchronous, pre-validation plugin that fires on-create.  It retrieves a 'last used ID' from another entity, increments it and saves back the updated 'last used ID'.  I was trying to use the RowVersion property to ensure another instance hadn't updated the 'last used ID' before I tried to update it, but RowVersion is empty every time.  I've double-checked the metadata to be sure that IsOptimisticConcurrencyEnabled is set to true for the entity that I'm trying to get RowVersion.

My workaround was to lock the part of my plugin that retrieves, increments, and re-saves the last used ID as a critical section (C# lock statement).  Unfortunately, Fede, I don't think this applies to your situation, but maybe it will spark some ideas or help someone else that comes across this post.

Reply
Pawel Gradecki responded on 12 Dec 2016 5:15 PM
My Badges
Suggested Answer

Hi,

According to the documentation that you provided you have to retrieve the entity. You are not retrieving entity, you are using "Target" object in a plugin, which is not the same (target is passed to the plugin in the context and does not contain all the information that resides in the entity). Try to Retrieve this entity in this plugin - RowVersion should be filled.

Reply
Alagu nellaikumar.S responded on 12 Dec 2016 11:46 PM
My Badges
Suggested Answer

Hi

Why don't you try post image?

Reply
Patrick V responded on 15 Dec 2016 2:13 PM
Suggested Answer

I'm seeing this in CRM 2016 on-prem as well.  I have a plugin that generates a custom ID for an entity.  I have a non-sandbox, synchronous, pre-validation plugin that fires on-create.  It retrieves a 'last used ID' from another entity, increments it and saves back the updated 'last used ID'.  I was trying to use the RowVersion property to ensure another instance hadn't updated the 'last used ID' before I tried to update it, but RowVersion is empty every time.  I've double-checked the metadata to be sure that IsOptimisticConcurrencyEnabled is set to true for the entity that I'm trying to get RowVersion.

My workaround was to lock the part of my plugin that retrieves, increments, and re-saves the last used ID as a critical section (C# lock statement).  Unfortunately, Fede, I don't think this applies to your situation, but maybe it will spark some ideas or help someone else that comes across this post.

Reply

SBX - Two Col Forum

SBX - Migrated JS