Avoid data loss with Optimistic Concurrency in Dynamics CRM 2015 Online

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 thumb Avoid data loss with Optimistic Concurrency in Dynamics CRM 2015 Online

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%5B6%5D thumb Avoid data loss with Optimistic Concurrency in Dynamics CRM 2015 Online

You set the UpdateRequest as below:

clip image002%5B8%5D thumb Avoid data loss with Optimistic Concurrency in Dynamics CRM 2015 Online

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.

image thumb Avoid data loss with Optimistic Concurrency in Dynamics CRM 2015 OnlineThe 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.

image thumb 1 Avoid data loss with Optimistic Concurrency in Dynamics CRM 2015 OnlineThe ConcurrencyBehavior is of enum type and has three members:

clip image002%5B10%5D thumb Avoid data loss with Optimistic Concurrency in Dynamics CRM 2015 Online

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.

This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.

Magnetism Solutions Dynamics CRM Blog