Optimistic Concurrency Control from Dynamics CRM 2015 Update 1

By | April 20, 2016

What is the need of Concurrency Control?

Systems such as Dynamics CRM are used by multiple users. When two or more CRM users need to update a same record at the same instance, then the changes made by the last user will be saved. There is no such concurrency control mechanism to stop users from overriding the changes made by others. This finally results into inconsistent data when the same fields are updated by the users.

Let’s take a look how the sanctity and the consistency of the data is maintained with the help of the concurrency control mechanism.

In general, there are two Concurrency Control Mechanisms:

A] Pessimistic concurrency control

When a user is modifying the data, another individual can’t access the same data at same time because the access has been locked.

B] Optimistic concurrency control

The record can be accessed but if you try to edit the record and the record is already modified since u last accessed it the modifications are not updated.

Since the release of CRM 2015 update 1, Optimistic Concurrency control method is been introduced while updating and deleting the record through UpdateRequest & DeleteRequest respectively.

Consider the account entity record, “A. Datum Corporation (sample)” with the details as shown in the screenshot below.Optimistic Concurrency Control from Dynamics CRM

We are retrieving this record from the code for updating it through UpdateRequest as you can see in the code block below:

//Retrieve Account Record "A. Datu Corporation (sample)"
Entity accountRecord = _service.Retrieve("account", new Guid("182EFFB3-10EC-E511-80E2-C4346BAD87C8"), new ColumnSet(true));
                
//Code to Update the Record
 //Initialize new Object to update
  Entity updateRecord = new Entity();

  updateRecord.Id = accountRecord.Id;

  updateRecord.LogicalName = accountRecord.LogicalName;

  //Set the RowVersion Property of the Updating record
  updateRecord.RowVersion = accountRecord.RowVersion;

  //Update Name
  updateRecord["name"] = "Account Changed";

  UpdateRequest updateReq = new UpdateRequest();

  updateReq.Target = updateRecord;

//Set the Concurrency Behaviour before executing the Update request & match the Row Versions of the record before executing the Request
updateReq.ConcurrencyBehavior = ConcurrencyBehavior.IfRowVersionMatches;

  // Do the update.
  UpdateResponse updateResponse = (UpdateResponse)_service.Execute(updateReq);

 Now, suppose before UpdateRequest is executed the name of the record is changed in CRM at the same time by a different user.
 Then _service.Execute() will give an Exception: ‘The version of the existing record doesn't match the RowVersion property provided.’ and it would not be executed successfully.
If you want to forcibly perform the Update operation through code then simply change this line as mentioned below:
//Set the Concurrency Behaviour before executing the Update request to Ignore the check and update the record successfully
updateReq.ConcurrencyBehavior = ConcurrencyBehavior.AlwaysOverwrite;

The same control can be added while deleting the record using DeleteRequest as mentioned below:
//Create Delete Request
DeleteRequest delReuest = new DeleteRequest()
{
  Target = accountRecord.ToEntityReference(),
  ConcurrencyBehavior = ConcurrencyBehavior.IfRowVersionMatches
};

_service.Execute(delReuest);

Conclusion:

Therefore, it is easier and simple to prevent the users from overriding the changes made by other users’ at the same time.

Hold… Are you planning to buy Bing Maps license for your Dynamics CRM? No need – Try Maplytics!