Concurrency Control in Microsoft Dynamics CRM 2013

Dynamics CRM 2013 still has a data concurrency issue. This post will guide you on how to introduce an optimistic concurrency control mechanism using JavaScript in the least obtrusive way possible.

Introduction

When I got my hands on the Microsoft Dynamics CRM 2013 Beta, the first thing I tried was to save two versions of the same record concurrently to check whether CRM 2013 implements any concurrency control mechanism.Unfortunately, CRM 2013 is not any different to its predecessors in this field.
Following-up from my popular post about concurrency control in CRM 2011, I’ve decided to write a new post for CRM 2013 with some updates leveraging the latest 2013 features.
The following post will describe how to create a client side concurrency control mechanism in CRM 2013. The post also demonstrates how to call the SOAP and the REST web services using JavaScript in a cross browser friendly way. The required JavaScript file can be download from my SkyDrive.

What is the concurrency problem?

Systems like Dynamics CRM are built to be used by a large number of users. When two or more users are updating the same record, the last person to save the record “wins”. Dynamics CRM does not have any concurrency control mechanism to warn or stop the users from overriding other users’ changes. This can potential result in inconsistent data especially if the users are updating the same fields.

What is new in CRM 2013

CRM 2013 offers some new and exciting features that we can use to enhance the user experience. In this post, I will be leveraging the new Auto-Save and Form Notification features to display a discrete message on the form when the record has been updated elsewhere. 
The JavaScript has also been updated and improved to work cross-browser.

How to solve the problem

The idea is to call the REST services and check that the value of the ModifiedOn field. If the value is greater than when the form was loaded, then the records has been changed. When auto-saving the check will display a form notification that the records has been updated by another user. If the form is saving, then a confirmation message will appear to save or cancel the event.

The JavaScript

The JavaScript code has been simplified in this version. All you’ll have to do to set it up is to call the Init method on load.
The JavaScript file can be downloaded from my SkyDrive.
I am using some REST OData and SOAP calls in in my script as well as some CRM 2013 specific features such as the Xrm.Page.ui.setFormNotification to display a notification and Xrm.Utility.confirmDialog to display a confirmation dialogue. Make sure you are using CRM 2013 Beta Refresh 6.0.0.448 or later. Older versions might not have the implementation of Xrm.Utility.confirmDialog in which case it should be replaced by the usual JavaScript confirm.
I’ll explain the JavaScript library in details in a future post.

How to set it up

Just add the JavaScript as a web resource to the form and set the OnLoad event to call the Init method in this JavaScript.

Using it

To test the functionality open an existing record in a new window (you can use the pop out button in the top right corner), copy the URL and paste is a new window (or use the CTRL+N shortcut in IE) change the record in one of the windows and try saving it in the other.
If the “dirty” record is un-saved, the auto-save functionality will kick in and display the following discrete message on the form.
A form notification type of message on an account stating "Seems like this record has been updated by Rami Mounla. AutoSave has been aborted."
If the user presses the save button, a pop-up will appear.
A JavaScript confirmation message on an account stating "Seems like this record has been updated by Rami Mounla. Are you sure you still want to save it."

 

Similarly if a user tried to close the browser a “save and close” event is triggered and a pop-up message will appear. In CRM 2013 whenever a user closes the browser a “save and close” event seems to be triggers as the “save and close” button has been removed. However this functionality does not seem to work with Chrome.

A JavaScript confirmation message on an account stating "Seems like this record has been updated by Rami Mounla. Do you want to save while closing?"

Disclaimer

The code this post is for reference only. It is provided as-is with no guarantees. This should not be used in production systems without being reviewed and completed. The code was tested on IE10 Firefox and Chrome with CRM 2013 Beta Refresh 6.0.0.448.
Rami Mounla

Leave a Reply

Your email address will not be published. Required fields are marked *