Avoid data loss with Optimistic Concurrency in Dynamics CRM 2015 Online

Ahmed Anwar, 13 October 2015

In large organisations with many users, data loss is likely to happen. This can occur when multiple users are updating the same record, which means that the changes one user makes can get overwritten by another user.

Imagine two users receive customer support tickets from customers and take actions by retrieving and updating these tickets using a desktop application. Both users open the same Ticket record and one of them updated it by increasing the Priority Level field. The other user took a little bit longer to update the record. When the second user submitted his/her changes, all submitted data by the first user are lost. Dynamics CRM 2015 Online Update 1 addresses this issue for update or delete requests by setting the concurrency behaviour.

clip_image002

The Entity class has a new property called RowVersion, which is set when a record is retrieved from CRM. This property is of type “long” and is increased each time you update a record.

The following code snippet illustrates the use of the RowVersion property. We first retrieve a ticket record from CRM and then update the record.

clip_image002[6]

You set the UpdateRequest as below:

clip_image002[8]

When you execute the UpdateRequest, CRM compares the row version. If it doesn’t match, the CRM platform will throw an error.

I’ve written a sample Console Application and ran two instances to retrieve and update the same record.

imageThe instance with Agent 1 succeeded to update the record but the second instance with Agent 2 failed because both retrieved the same record with same row version.

imageThe ConcurrencyBehavior is of enum type and has three members:

clip_image002[10]

If you want to deliberately take the latest changes, then use AlwaysOverwrite.

For more information about how to use this enum in other contexts like plugins or workflows, check the MSDN documentation here.