Dynamics 365 CE – Optimistic Concurrency – Handling Concurrent Transactions

Hi Everyone,

Let’s discuss something on Optimistic Concurrency today. 

What are Concurrent Transaction?

When two or more users need to update a same record at the same time, the changes made by the last user will be saved and this results data loss. The operations performing by users on the record at the same are known as Concurrent Transactions.

In general, there are two ways to control this behavior.

1) Pessimistic Concurrency Control
When a user is modifying the data, another user can’t access the same data at same time as the access has been locked on that data.
2) Optimistic Concurrency Control
This feature provides the ability for your applications to detect whether an entity record has changed on the server in the time between when your application retrieved the record and when it tries to update or delete that record.

On all entities, you will see a OOB filed “Version Number” of type Timestamp and it gets updated by the system every time when the record updates.

In the below code, I am retrieving the record and trying to update but before updating the record using the code I am going update the record manually in CRM. The below code will throw the exception as below.

“The version of the existing record doesn’t match the RowVersion property provided.”

                // Retrieving the account record.
                Entity entAccount = crmService.Retrieve(“account”, new Guid(“4921991e-162d-ea11-a813-000d3a59f4b3”), newColumnSet(new string[] { “name” }));
                // Creating a new object and updating the account record that was retrieved above by setting Concurrency Behavior to If RowVersionMatches.
                Entity entAccountToUpdate = new Entity(“account”);
                entAccountToUpdate.Id = entAccount.Id;
                entAccountToUpdate[“name”] = “My Account Name”;
                // Setting up the Row Version.
                entAccountToUpdate.RowVersion = entAccount.RowVersion;
                UpdateRequest accountReq = new UpdateRequest()
                    Target = entAccountToUpdate,
                    ConcurrencyBehavior = ConcurrencyBehavior.IfRowVersionMatches
                UpdateResponse accountUpdateResponse = (UpdateResponse)crmService.Execute(accountReq);
            catch (FaultException fx)
                switch (fx.Detail.ErrorCode)
                    case -2147088254: // ConcurrencyVersionMismatch
                    case -2147088253: // OptimisticConcurrencyNotEnabled
                        throw new InvalidOperationException(fx.Detail.Message);
                    case -2147088243: // ConcurrencyVersionNotProvided
                        throw new ArgumentNullException(fx.Detail.Message);
                        throw fx;

Hope this helps.

Happy 365’ing


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s