Optimistic Concurrency in Dynamics 365

By | September 25, 2017

Introduction:

Very often, we need to retrieve an entity to update a field but due to concurrency issue, the entity is already updated, and we are stuck. In scenarios like this, we can use Optimistic Concurrency to determine whether an entity was modified since it was last retrieved!

Recently, while working with one of our applications, we faced the concurrency issue where we retrieved the data to be displayed on the form but, in some cases, the data was already modified through the plugin in the background. So, when the update action was fired, it was not executed on the latest data! So, we needed to check whether the data was modified since the last retrieval.

Using ‘Etag’ and ‘If-Match Header’ to detect modifications since the last retrieval:

Microsoft Dynamics 365 provides a weakly validating Property named ‘@odata.etag’ that is automatically returned along with each retrieved entity record as seen below;

Optimistic Concurrency in Dynamics 365This property is known as ‘ETag’ and is changed wherever modification is done on that entity record.

We used this property to check whether the data we have is the latest data. For this we added this ‘ETag’ value in the If-Match header in the update request as seen below;

Optimistic Concurrency in Dynamics 365If the data is not updated since we have last retrieved it, we get a 204 (No Content) status, and if the data is updated we get the response as seen below;

Optimistic Concurrency in Dynamics 365Based on the response, we can continue with the update operation or perform the retrieve action again to get the latest data.

Conclusion:

We can use the ETag property of the retrieved data and pass the same in the IF-Match header to check if the data we are working on is the latest data to avoid the concurrency issues.

Copy_clone Dynamics 365_CRM Records

Leave a Reply