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