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 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
Using 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.