
What is Optimistic Concurrency?
Is SDK feature used to avoid potential data loss, it is made possible by the ConcurrencyBehavior option (Specifies the type of optimistic concurrency behavior that should be performed by the Web service when processing the request) of UpdateRequest and DeleteRequest message.
Why Optimistic Concurrency?
Imagine this scenario:
two users access the same Contact record, And Customer ask to change the address of this contact,the first user retrieve record and change it but still not saved yet, then the customer ask to change the address again, and the last user save the changes, however the first user still open the record. then the first user save changes but with old address.
it’s possible to use Optimistic Concurrency control to avoid potential data loss or data mismatch. The idea is simple, the entities now have a property called RowVersion (auto incremental number) which stores the retrieved version of the record. When an Update or Delete request is executed (and the ConcurrencyBehavior has been explicitly configured), Dynamics 365 will compare if the record RowVersion is the same than the one stored in the database and rollback the operation if they’re not.
How to enable Concurrency Behavior for Entities?
Concurrency version is enabled by default for all custom entities. For Out of the box entities, you can use IsOptimisticConcurrencyEnabled method to check if an entity is supporting this behavior.
How to use Concurrency Behavior?
As of now the only way to enable concurrency is by SDK call. There is no way to configure Concurrency Behavior through UI.
How to use Concurrency Behavior Programatically?
ConcurrencyBehavior option is available for UpdateRequest and DeleteRequest message.
Below is the sample code to enable concurrency behavior:
// Retrieve a contact record
var currentContact = serviceProxy.Retrieve("contact", contactid, new ColumnSet("address1"));
if (currentContact != null)
{
// Create an Entity object
Entity updatedContact = new Entity()
{
LogicalName = currentContact.LogicalName,
Id = currentContact.Id,
RowVersion = currentContact.RowVersion
};
// Update the Contact.
updatedContact["address1"] = "test address";
// Set the concurrency behavior to check for a row version match. If row version does not match
//It will throw exception otherwise contact record will gets updated.
UpdateRequest contactReq = new UpdateRequest()
{
Target = updatedContact,
ConcurrencyBehavior = ConcurrencyBehavior.IfRowVersionMatches
};
// Perform the update operation.
try
{
UpdateResponse accountUpdateResponse = (UpdateResponse) serviceProxy.Execute(contactReq);
}
catch(FaultException<OrganizationServiceFault> ex)
{
if(ex.Code == OPTIMISTIC_CONCURRENCY_VIOLATION)
{
// TODO: Logic
}
}
}
ConcurrencyBehavior option is also available for DeleteRequest message, which provides a similar logic.
Concurrency Behavior is available only for the Below CRM versions:
- CRM Online 2015 Update 1.
- CRM 2016 Online & OnPremise
- Dynamics 365 Online & OnPremise
Notes:
the RowVersion property is never passed to the plugin target entity and therefore there’s no way of comparing them.
therefore Try to Retrieve this entity in this plugin instead of using targetEntity
Comments
No comments yet.
You must login to add your comment
Login now