{"id":6626,"date":"2017-09-25T18:26:57","date_gmt":"2017-09-25T12:56:57","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=6626"},"modified":"2017-09-25T18:26:57","modified_gmt":"2017-09-25T12:56:57","slug":"optimistic-concurrency-in-dynamics-365","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2017\/09\/optimistic-concurrency-in-dynamics-365\/","title":{"rendered":"Optimistic Concurrency in Dynamics 365"},"content":{"rendered":"<h4 style=\"text-align: justify;\"><strong>Introduction:<\/strong><\/h4>\n<p style=\"text-align: justify;\">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!<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<h4 style=\"text-align: justify;\"><strong>Using \u2018Etag\u2019 and \u2018If-Match Header\u2019 to detect modifications since the last retrieval:<\/strong><\/h4>\n<p style=\"text-align: justify;\">Microsoft Dynamics 365 provides a weakly validating Property named \u2018@odata.etag\u2019\u00a0that is automatically returned along with each retrieved entity record as seen below;<\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/09\/Optimistic-Concurrency-in-Dynamics-3651.png\"><img decoding=\"async\" class=\"alignnone wp-image-6629 \" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/09\/Optimistic-Concurrency-in-Dynamics-3651.png\" alt=\"Optimistic Concurrency in Dynamics 365\" width=\"826\" height=\"103\" \/><\/a>This property is known as \u2018ETag\u2019 and is changed wherever modification is done on that entity record.<\/p>\n<p style=\"text-align: justify;\">We used this property to check whether the data we have is the latest data. For this we added this \u2018ETag\u2019 value in the If-Match header in the update request as seen below;<\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/09\/Optimistic-Concurrency-in-Dynamics-3652.png\"><img decoding=\"async\" class=\"alignnone  wp-image-6630\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/09\/Optimistic-Concurrency-in-Dynamics-3652.png\" alt=\"Optimistic Concurrency in Dynamics 365\" width=\"824\" height=\"140\" \/><\/a>If 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;<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/09\/Optimistic-Concurrency-in-Dynamics-3653.png\"><img decoding=\"async\" class=\"alignnone  wp-image-6631\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/09\/Optimistic-Concurrency-in-Dynamics-3653.png\" alt=\"Optimistic Concurrency in Dynamics 365\" width=\"826\" height=\"229\" \/><\/a>Based on the response, we can continue with the update operation or perform the retrieve action again to get the latest data.<\/p>\n<h4 style=\"text-align: justify;\"><strong>Conclusion:<\/strong><\/h4>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.inogic.com\/product\/productivity-pack\/click-2-clone-microsoft-dynamics-crm-records\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"alignnone wp-image-6193 \" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/08\/Click2Clone-One-Click-Productivity-App-to-Copy_clone-Dynamics-365_CRM-Records.png\" alt=\"Copy_clone Dynamics 365_CRM Records\" width=\"824\" height=\"206\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2017\/09\/optimistic-concurrency-in-dynamics-365\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":6628,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16,19],"tags":[533,592,1279],"class_list":["post-6626","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","category-dynamics-crm","tag-dynamics-365","tag-dynamics-crm","tag-optimistic-concurrency"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/6626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=6626"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/6626\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/6628"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=6626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=6626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=6626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}