{"id":2630,"date":"2016-04-15T17:24:44","date_gmt":"2016-04-15T11:54:44","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=2630"},"modified":"2021-08-25T11:49:56","modified_gmt":"2021-08-25T11:49:56","slug":"how-to-delete-recordfield-value-using-the-web-api-in-dynamics-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2016\/04\/how-to-delete-recordfield-value-using-the-web-api-in-dynamics-crm\/","title":{"rendered":"How to delete record\/field value using the Web API in Dynamics CRM"},"content":{"rendered":"<p><strong>Introduction:<\/strong><\/p>\n<p>As we all know, the Web API has been introduced in Microsoft Dynamics CRM 2016. It offers a great development experience across many devices, programming languages and platforms.<\/p>\n<p>In our earlier <a href=\"https:\/\/www.inogic.com\/blog\/2015\/12\/execute-fetchxml-using-web-api-in-dynamics-crm-2016\/\" target=\"_blank\" rel=\"noopener noreferrer\">blog<\/a>, we have seen how to create and retrieve records using the Web API. Now, let\u2019s take a look of how to delete a Lookup field value in CRM.<\/p>\n<p><strong>Clear Entity Reference [Lookup] Field Value:<\/strong><\/p>\n<p><strong>We are trying to Clear the lookup field value or set the value of the lookup field as null using the Patch action but it does not work as expected. So there is an alternate way to clear the lookup field value by using the DELETE action.<\/strong><\/p>\n<p>If we need to clear the lookup field value using DELETE action then we need to pass the Record GUID with the logical name of lookup field along with the <strong>\u2018$ref\u2019<\/strong> keyword.<\/p>\n<p>Here\u2019s is the code snippet.<\/p>\n<pre class=\"lang:default decode:true \">\/\/Function to delete the entity refrence field value \r\nfunction DeleteEntityRefField() {\r\n\r\n    var functionName = \"DeleteEntityRefField\";\r\n    try {\r\n        \r\n        \/\/get Server url\r\n        var serverURL = Xrm.Page.context.getClientUrl();\r\n\r\n        \/\/create XML request object \r\n        var xhr= new XMLHttpRequest();\r\n\r\n        \/\/below line is used to delete the entity ref field (lookup) from the account record\r\n        xhr.open(\"DELETE\", serverURL + \"\/api\/data\/v8.0\/accounts(1DD18913-11CB-E511-80D2-C4346BDC11C1)\/primarycontactid\/$ref\", 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 \/* complete *\/) {\r\n                xhr.onreadystatechange = null;\r\n                if (this.status == 204) {\r\n\r\n                    \/\/show alert message \r\n                    alert('Field Value Deleted');\r\n                }\r\n                else {\r\n                    var error = JSON.parse(this.response).error;\r\n\r\n                    \/\/show error message\r\n                    alert(error.message);\r\n                }\r\n            }\r\n        };\r\n        xhr.send();\r\n    } catch (e) {\r\n        alert(functionName + (e.message || e.description));\r\n    }\r\n}<\/pre>\n<p>Later, the above code is used to clear\/delete the entity reference (lookup) field value. To specify the field is lookup field, we need to use entity reference \u2018<strong>\/$ref<\/strong>\u2019 after the field name.<\/p>\n<p><strong>Delete String\/Date\/OptionSet\/Currency Field Value:<\/strong><\/p>\n<p>We can also clear the single entity attribute value of type string\/Date\/Option Set\/Currency using the DELETE action. To perform this action we need to pass the field\u2019s logical name and the record GUID in the Web API URL section. Here\u2019s is the code which will help you understand what we are trying to say.<\/p>\n<pre class=\"lang:default decode:true \">\/\/Function to delete the entity reference field value \r\nfunction DeleteFieldValue() {\r\n\r\n    var functionName = \"DeleteEntityRecord\";\r\n    try {\r\n        \r\n        \/\/get Server url\r\n        var serverURL = Xrm.Page.context.getClientUrl();\r\n\r\n        \/\/create XML request object \r\n        var xhr = new XMLHttpRequest();\r\n\r\n        \/\/below line ios used to delete the particular field value \r\n        xhr.open(\"DELETE\", serverURL + \"\/api\/data\/v8.0\/accounts(1DD18913-11CB-E511-80D2-C4346BDC11C1)\/telephone1\", 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 \/* complete *\/) {\r\n                xhr.onreadystatechange = null;\r\n                if (this.status == 204) {\r\n\r\n                    \/\/show alert message \r\n                    alert('Field Value Deleted');\r\n                }\r\n                else {\r\n                    var error = JSON.parse(this.response).error;\r\n\r\n                    \/\/show error message\r\n                    alert(error.message);\r\n                }\r\n            }\r\n        };\r\n        xhr.send();\r\n    } catch (e) {\r\n        alert(functionName + (e.message || e.description));\r\n    }\r\n}<\/pre>\n<p>From the above code the <strong>\u2018Phone (telephone1)\u2019<\/strong> field value from the account record will be cleared.<\/p>\n<p><strong>Deleting an Entity Record:<\/strong><\/p>\n<p>Let\u2019s consider <strong>\u2018Accounts\u2019<\/strong> as the entity for which the record needs to be deleted. You have to pass the record GUID as a parameter in the WEB API URL in order to delete the entity record.<\/p>\n<p>Check out the code as written below.<\/p>\n<pre class=\"lang:default decode:true \">\/\/Function to delete the entity refrence field value \r\nfunction DeleteEntityRecord() {\r\n\r\n    var functionName = \"DeleteEntityRecord\";\r\n    try {\r\n        \r\n        \/\/get Server url\r\n        var serverURL = Xrm.Page.context.getClientUrl();\r\n\r\n        \/\/create Xml request object \r\n        var xhr = new XMLHttpRequest();\r\n\r\n        \/\/below line is used to delete the entity record  \r\n        xhr.open(\"DELETE\", serverURL + \"\/api\/data\/v8.0\/accounts(1DD18913-11CB-E511-80D2-C4346BDC11C1)\", 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 \/* complete *\/) {\r\n                xhr.onreadystatechange = null;\r\n                if (this.status == 204) {\r\n\r\n                    \/\/show alert message \r\n                    alert('Field Value Deleted');\r\n                }\r\n                else {\r\n                    var error = JSON.parse(this.response).error;\r\n\r\n                    \/\/show error message\r\n                    alert(error.message);\r\n                }\r\n            }\r\n        };\r\n        xhr.send();\r\n    } catch (e) {\r\n        alert(functionName + (e.message || e.description));\r\n    }\r\n}<\/pre>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>On a concluding note, it is actually simple to delete an entity record using Web API. You should ensure that the right record GUID and the field\u2019s logical name are passed as parameters.<\/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\">Free 70% of storage space in CRM with Attachment Management Apps!<\/div><\/div><\/h2>\n<p><em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/attach-2-dynamics-365-crm-upload-multiple-files-sharepoint-cloud-storage\" target=\"_blank\" rel=\"noopener noreferrer\">Attach2Dynamics<\/a> &#8211; Store and manage documents\/attachments in cloud storage of your choice &#8211; SharePoint, Dropbox or Azure Blob Storage from within Dynamics 365 CRM.<\/em><br \/>\n<em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/dynamics-365-crm-sharepoint-security-metadata-sync\" target=\"_blank\" rel=\"noopener noreferrer\">SharePoint Security Sync<\/a><\/strong> \u2013 Robust and secure solution to integrate Dynamics 365 CRM and SharePoint Security Sync thereby ensuring secure access to confidential documents stored in SharePoint.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: As we all know, the Web API has been introduced in Microsoft Dynamics CRM 2016. It offers a great development experience across many devices, programming languages and platforms. In our earlier blog, we have seen how to create and retrieve records using the Web API. Now, let\u2019s take a look of how to delete\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2016\/04\/how-to-delete-recordfield-value-using-the-web-api-in-dynamics-crm\/\">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,65],"tags":[],"class_list":["post-2630","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-dynamics-crm-2016","category-webapi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/2630","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=2630"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/2630\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=2630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=2630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=2630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}