{"id":3681,"date":"2016-10-05T17:53:39","date_gmt":"2016-10-05T12:23:39","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=3681"},"modified":"2022-09-02T16:32:34","modified_gmt":"2022-09-02T11:02:34","slug":"retrieve-update-and-delete-record-using-alternate-key-in-dynamics-crm-web-api","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2016\/10\/retrieve-update-and-delete-record-using-alternate-key-in-dynamics-crm-web-api\/","title":{"rendered":"Retrieve, Update and Delete Record Using Alternate Key in Dynamics CRM Web API"},"content":{"rendered":"<p>Before Alternate key concept was introduced in Dynamics CRM it used to be the only GUID of the record which was used to retrieve, update or delete any record. Hence we had to get the record GUID first for any retrieve, update or delete operation. But with the introduction of Alternate keys concept this overhead of getting record GUID went away as we got an alternative way to create any field as Alternate Key and use it. Let\u2019s see how we can perform these operations using Web API now with the help of Alternate keys.<\/p>\n<p><strong>Delete operation in Web API using Alternate key:<\/strong><\/p>\n<pre class=\"lang:default decode:true \">function DeleteEntityRecord() {\r\n    var functionName = \"DeleteEntityRecord\";\r\n    try {\r\n        \/\/get Server url\r\n        var serverURL = Xrm.Page.context.getClientUrl();\r\n        \r\n        var xhr = new XMLHttpRequest();\r\n        \r\n        xhr.open(\"DELETE\", serverURL + \"\/api\/data\/v8.0\/accounts(accountnumber='AFFSE9IK\u2032)')\", true);\r\n        xhr.setRequestHeader(\"Accept\", \"application\/json\");\r\n        xhr.setRequestHeader(\"Content-Type\", \"application\/json; charset=utf-8\");\r\n        xhr.setRequestHeader(\"OData-MaxVersion\", \"4.0\");\r\n        xhr.setRequestHeader(\"OData-Version\", \"4.0\");\r\n        xhr.onreadystatechange = function () {\r\n            if (this.readyState == 4) {\r\n                xhr.onreadystatechange = null;\r\n                if (this.status == 204) {\r\n                    \/\/show alert\r\n                    Xrm.Utility.alertDialog('Record Deleted Successfully.');\r\n                }\r\n                else {\r\n                    var error = JSON.parse(this.response).error;\r\n                    \/\/show error\r\n                    Xrm.Utility.alertDialog(error.message);\r\n                }\r\n            }\r\n        };\r\n        xhr.send();\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(functionName + (e.message || e.description));\r\n    }\r\n}<\/pre>\n<p>In above operation \u201caccountnumber\u201d is Alternate key using which we have performed delete operation.<\/p>\n<p><strong>Update operation in Web API using Alternate key:<\/strong><\/p>\n<pre class=\"lang:default decode:true \">function UpdateEntityRecord() {\r\n    var functionName = \"UpdateEntityRecord\";\r\n    try {\r\n        \/\/get Server url\r\n        var serverURL = Xrm.Page.context.getClientUrl();\r\n\r\n        var xhr = new XMLHttpRequest();\r\n\r\n        xhr.open(\"PATCH\", serverURL + \"\/api\/data\/v8.0\/contacts(emailaddress1='Adrian@adventure-works.com')\", true);\r\n        xhr.setRequestHeader(\"Accept\", \"application\/json\");\r\n        xhr.setRequestHeader(\"Content-Type\", \"application\/json; charset=utf-8\");\r\n        xhr.setRequestHeader(\"OData-MaxVersion\", \"4.0\");\r\n        xhr.setRequestHeader(\"OData-Version\", \"4.0\");\r\n        xhr.onreadystatechange = function () {\r\n            if (this.readyState == 4) {\r\n                xhr.onreadystatechange = null;\r\n                if (this.status == 204) {\r\n                    \/\/show alert\r\n                    Xrm.Utility.alertDialog('Record Updated Successfully.');\r\n                }\r\n                else {\r\n                    var error = JSON.parse(this.response).error;\r\n                    \/\/show error\r\n                    Xrm.Utility.alertDialog(error.message);\r\n                }\r\n            }\r\n        };\r\n        var objContact = {};\r\n        objContact.firstname = \"Sam\";\r\n        objContact.lastname = \"Dsouza\";\r\n\r\n\r\n        var body = JSON.stringify(objContact);\r\n        xhr.send(body);\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(functionName + (e.message || e.description));\r\n    }\r\n}<\/pre>\n<p>In above operation \u201cemailaddress1\u201d is Alternate key using which we have performed update operation.<\/p>\n<p><strong>Retrieve operation in Web API using Alternate key:<\/strong><\/p>\n<pre class=\"lang:default decode:true \">function getEntityByAlternateKey() {\r\n    try {\r\n        var clientUrl = Xrm.Page.context.getClientUrl();\r\n        var xhr = new XMLHttpRequest();\r\n        \r\n        xhr.open(\"GET\", encodeURI(clientUrl + \"\/api\/data\/v8.0\/contacts(mobilephone=\u20199568565458\u2032)\"), true);\r\n\r\n        \/\/ For Multiple Alternate keys use like this\r\n        \/\/req.open(\"GET\", encodeURI(clientUrl + \"\/api\/data\/v8.0\/contacts(mobilephone=\u20199568565458\u2032,emailaddress1=\u2019Adrian@adventure-works.com\u2019)\"), true);\r\n\r\n        xhr.setRequestHeader(\"Accept\", \"application\/json\");\r\n        xhr.setRequestHeader(\"Content-Type\", \"application\/json; charset=utf-8\");\r\n        xhr.setRequestHeader(\"OData-MaxVersion\", \"4.0\");\r\n        xhr.setRequestHeader(\"OData-Version\", \"4.0\");\r\n        xhr.onreadystatechange = function () {\r\n            if (this.readyState == 4) {\r\n                xhr.onreadystatechange = null;\r\n                if (this.status == 200) {\r\n                    var data = JSON.parse(this.response);\r\n                    var dat = data.value;\r\n                    for (var i = 0; i &lt; dat.length; i++) {\r\n                        var contId = dat[i].contactid;\r\n                        Xrm.Utility.alertDialog(\"Entity Id : \" + contId);\r\n                    }\r\n                }\r\n                else {\r\n                    var error = JSON.parse(this.response).error;\r\n                    Xrm.Utility.alertDialog(\"Error retrieving Entity- \" + error.message);\r\n                }\r\n            }\r\n        };\r\n\r\n        xhr.send();\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(\"Error in getting Entity by alternate key \u2013 \" + e.description);\r\n    }\r\n}<\/pre>\n<p>In above operation \u201cmobilephone\u201d is Alternate key using which we have performed retrieve operation.<\/p>\n<p>In this way Alternate keys concept can be used for different operations like to retrieve, update, or delete any record.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">Generate Your Own New Leads Within Microsoft Dynamics 365 CRM<\/div><\/div><\/h2>\n<p><em>Contact us for a <a href=\"https:\/\/www.maplytics.com\/maplytics-download\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">demo<\/a> to know more about how <a href=\"https:\/\/www.maplytics.com\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">Maplytics<\/a> can help you to generate new leads from within Microsoft Dynamics 365 CRM.<\/em><\/p>\n<p><em><a href=\"https:\/\/www.maplytics.com\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">Maplytics<\/a> is a 5-star rated, preferred business app on the <a href=\"https:\/\/appsource.microsoft.com\/en-us\/product\/dynamics-365\/inogic.f6f3c73f-29de-4fa8-a396-87ea8a07b6c4?tab=Overview\" target=\"_blank\" rel=\"noopener\">Microsoft AppSource<\/a> that is Certified for Microsoft Dynamics 365 (CfMD) and comes with powerful features like Appointment Planning, Sales Routing, Territory Management, Heat Maps, Geo-analytical Dashboards and more that empower organizations to add more value to their CRM data, improve sales &amp; service processes, and achieve high ROI.<\/em><\/p>\n<p><em>Get your <a href=\"https:\/\/www.maplytics.com\/maplytics-download\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">free trial<\/a> from our Website or <a href=\"https:\/\/appsource.microsoft.com\/en-us\/product\/dynamics-365\/inogic.f6f3c73f-29de-4fa8-a396-87ea8a07b6c4?tab=Overview\" target=\"_blank\" rel=\"noopener\">Microsoft AppSource<\/a>!<\/em><\/p>\n<p><em>&#8216;If data is the new oil, location intelligence is ??\u201d<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before Alternate key concept was introduced in Dynamics CRM it used to be the only GUID of the record which was used to retrieve, update or delete any record. Hence we had to get the record GUID first for any retrieve, update or delete operation. But with the introduction of Alternate keys concept this overhead\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2016\/10\/retrieve-update-and-delete-record-using-alternate-key-in-dynamics-crm-web-api\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"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":[19,24,33,65],"tags":[127,708,1145,1146,1222],"class_list":["post-3681","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-dynamics-crm-2016","category-javascript","category-webapi","tag-alternate-key-feature-in-web-api","tag-dynamics-crm-web-api","tag-microsoft-dynamics-crm-web-api","tag-microsoft-dynamics-crm-web-api-alternate-key-feature","tag-ms-dynamics-crm-web-api"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/3681","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=3681"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/3681\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=3681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=3681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=3681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}