{"id":11129,"date":"2018-01-29T17:14:29","date_gmt":"2018-01-29T11:44:29","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=11129"},"modified":"2021-12-06T14:54:20","modified_gmt":"2021-12-06T09:24:20","slug":"dynamics-365-v9-0-xrm-webapi-crud-operations-part-1","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2018\/01\/dynamics-365-v9-0-xrm-webapi-crud-operations-part-1\/","title":{"rendered":"Dynamics 365 v9.0: Xrm.WebApi \u2013 CRUD Operations Part \u2013 1"},"content":{"rendered":"<h2 style=\"text-align: justify;\"><strong>Introduction:<\/strong><\/h2>\n<p style=\"text-align: justify;\">Microsoft Dynamics 365 July Update, formally known as V9.0 has come up with lots of new and exciting features. No doubt these features have been applauded by end users and developers.<\/p>\n<p style=\"text-align: justify;\">One of the important enhancement that will be boon for developer is <strong>Xrm.WebApi<\/strong>. Earlier to v9.0, to perform CRUD operation in Dynamics CRM we need to create our own library or used other third party libraries or used XmlHttpRequest and ajax request. So <strong>Xrm.WebApi<\/strong> is a cool enhancement added in Dynamics 365 v9.0 which will help to make developers life simple.<\/p>\n<p style=\"text-align: justify;\">In this blog, we will discuss about how we can perform CRUD operations and work with all data types using newly added WebApi in Xrm namespace.<\/p>\n<h2 style=\"text-align: justify;\"><strong>Using Xrm.WebApi:<\/strong><\/h2>\n<p style=\"text-align: justify;\">Below is the example where we can used Xrm.WebApi.<\/p>\n<h2 style=\"text-align: justify; padding-left: 30px;\"><strong>1. Create:<\/strong><\/h2>\n<p style=\"text-align: justify; padding-left: 30px;\">To create the record, first we need to create the object of entity and then set the required fields and its value and then call <strong>Xrm.WebApi.createRecord <\/strong>function to create record in CRM. The parameters of this function are shown below;<\/p>\n<p style=\"text-align: justify; padding-left: 60px;\">1. Entity logical name<\/p>\n<p style=\"text-align: justify; padding-left: 60px;\">2. Entity object<\/p>\n<pre class=\"lang:default decode:true\">\/\/\/ This function is used to create a contact record\r\nfunction createContact() {\r\n    var contactObj = null;\r\n    try {\r\n       \r\n        \/\/ create the contact object\r\n        contactObj = new Object();\r\n        contactObj.firstname = \"Mike\";\r\n        contactObj.lastname = \"Morgan\";\r\n        contactObj.creditonhold = false;\r\n\r\n        \/\/set optionsetvalue\r\n        contactObj.accountrolecode = 2;\r\n        \/\/set the lookup value\r\n        contactObj[\"parentcustomerid_account@odata.bind\"] = \"\/accounts(A8A19CDD-88DF-E311-B8E5-6C3BE5A8B200)\"\r\n\r\n        Xrm.WebApi.createRecord(\"contact\", contactObj).then(function (result) {\r\n            \/\/get the guid of created record\r\n            var recordId = result.id;\r\n\r\n            \/\/below code is used to open the created record\r\n            var windowOptions = {\r\n                openInNewWindow: true\r\n            };\r\n            \/\/check if XRM.Utility is not null\r\n            if (Xrm.Utility != null) {\r\n\r\n                \/\/open the entity record\r\n                Xrm.Utility.openEntityForm(\"contact\", recordId, null, windowOptions);\r\n            }\r\n        },\r\n      function (error) {\r\n          Xrm.Utility.alertDialog(error.message);\r\n      });\r\n\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(e.message);\r\n    }\r\n}\r\n<\/pre>\n<h2 style=\"padding-left: 30px;\">\u00a0<strong>2.<\/strong>\u00a0<strong>Update:<\/strong><\/h2>\n<p style=\"padding-left: 30px;\">To update the record, first we need to create the object of the entity and then set the fields and its value that we need to update and then call <strong>Xrm.WebApi.updateRecord <\/strong>function to create the record in CRM. The parameters of this function are as shown below;<\/p>\n<p style=\"padding-left: 60px;\">1. Entity logical name<\/p>\n<p style=\"padding-left: 60px;\">2. Entity guid<\/p>\n<p style=\"padding-left: 60px;\">3. Entity object<\/p>\n<pre class=\"lang:default decode:true\">\/\/this function is used to update contact\r\nfunction updateContact() {\r\n    var ownerId = null;\r\n    var contactObj = null;\r\n    try {\r\n\r\n        \/\/ create the contact object\r\n        contactObj = new Object();\r\n\r\n        \/\/set the properties\r\n        contactObj.creditonhold = true;\r\n        \/\/set optionsetvalue\r\n        contactObj.accountrolecode = 3;\r\n        \/\/set the lookup value        \r\n        ownerId = \"1809D364-F311-4B0A-98D2-198B721B2061\";\r\n        contactObj[\"ownerid@odata.bind\"] = \"\/systemusers(\" + ownerId + \")\"\r\n\r\n        \/\/ pass entity logical name , entity guid, entity object\r\n        Xrm.WebApi.updateRecord(\"contact\", \"49A0E5B9-88DF-E311-B8E5-6C3BE5A8B200\", contactObj).then(function (result) {\r\n            Xrm.Utility.alertDialog(\"Record updated successfully.\");\r\n        },\r\nfunction (error) {\r\n            Xrm.Utility.alertDialog(error.message);\r\n        });\r\n\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(e.message);\r\n    }\r\n}\r\n<\/pre>\n<h2 style=\"padding-left: 30px;\"><strong>3.<\/strong>\u00a0<strong>Delete:<\/strong><\/h2>\n<p style=\"padding-left: 30px;\">To delete record from CRM use <strong>Xrm.WebApi.deleteRecord.<\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0The parameters of this function are shown below;<\/p>\n<p style=\"padding-left: 60px;\">1. Entity logical name<\/p>\n<p style=\"padding-left: 60px;\">2. Entity guid<\/p>\n<pre class=\"lang:default decode:true\">\/\/\/ This function is used to retrieve the Delete record\r\nfunction deleteRecord() {\r\n    try {\r\n        Xrm.WebApi.deleteRecord(\"contact\", \"8DA6E5B9-88DF-E311-B8E5-6C3BE5A8B200\")\r\n      .then(function (result) {\r\n          Xrm.Utility.alertDialog(\"Record deleted successfully.\");\r\n      },\r\n       function (error) {\r\n        Xrm.Utility.alertDialog(e.message);\r\n\r\n      });\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(e.message);\r\n    }\r\n}\r\n<\/pre>\n<h2 style=\"padding-left: 30px;\">4.\u00a0<strong>Retrieve Record<\/strong>:<\/h2>\n<p style=\"padding-left: 30px;\">In order to retrieve record we can use <strong>Xrm.WebApi.retrieveRecord.<\/strong> The parameters of this function are as shown below;<\/p>\n<p style=\"padding-left: 30px;\">1. Entity logical name<\/p>\n<p style=\"padding-left: 30px;\">2. Entity guid<\/p>\n<p style=\"padding-left: 30px;\">3. In last parameter pass following [select cause, filter cause etc.]\n<p>&nbsp;<\/p>\n<blockquote><p><em>Note: If you want to get the value of lookup field you have use _ as prefix and _value as suffix. For example for <strong>parentcustomerid<\/strong> use <strong>_parentcustomerid_value<\/strong><\/em><\/p><\/blockquote>\n<pre class=\"lang:default decode:true  \">\/\/\/ This function is used to retrieve the contact record\r\nfunction retrieveContact() {\r\n    try {\r\n\r\n        Xrm.WebApi.retrieveRecord(\"contact\", \"465B158C-541C-E511-80D3-3863BB347BA8\", \"$select=fullname,telephone1,preferredcontactmethodcode,createdon,_parentcustomerid_value,creditlimit\")\r\n            .then(function (data) {\r\n                retrieveContactSuccess(data);\r\n            },\r\n             function (error) {\r\n                Xrm.Utility.alertDialog(error.message);\r\n            });\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog (e.message);\r\n    }\r\n}\r\n\r\n\/\/\/retrieve success\r\nfunction retrieveContactSuccess(data) {\r\n   \r\n    try {\r\n        \/\/get the values \r\n\r\n        \/\/string\r\n        var fullname = data[\"fullname\"];\r\n\r\n        \/\/optionset\r\n        var typeCode = data[\"customertypecode\"];\r\n\r\n        \/\/lookup\r\n        var customerGuid = data[\"_parentcustomerid_value\"];\r\n        var customerName = data[\"_parentcustomerid_value@OData.Community.Display.V1.FormattedValue\"];\r\n        var customerEntityLogicalName = data[\"_parentcustomerid_value@Microsoft.Dynamics.CRM.lookuplogicalname\"];      \r\n\r\n        \/\/money\r\n        var creditLimit = data[\"creditlimit@OData.Community.Display.V1.FormattedValue\"];\r\n\r\n        \/\/date\r\n        var createdonFormattedValue = data[\"createdon@OData.Community.Display.V1.FormattedValue\"]; \/\/ gives date in following format 2017-09-30T21:10:19Z\r\n        \r\n        var createdon = data[\"createdon\"]; \/\/ gives date in following format 10\/1\/2017 2:40 AM\r\n\r\n        \/\/optionset\r\n        var preferredConMethod = data[\"preferredcontactmethodcode@OData.Community.Display.V1.FormattedValue\"];        \r\n\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(e.message);\r\n    }\r\n}\r\n<\/pre>\n<h2>\u00a0<strong>Conclusion<\/strong>:<\/h2>\n<p>Using Xrm.WebApi in Dynamics 365 v9.0, it is easy to perform CURD operation without performing AJAX request or creating own library or using third party libraries.<\/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\">One Pic = 1000 words! Analyze data 90% faster with visualization apps!<\/div><\/div><\/h2>\n<p style=\"text-align: left;\"><em>Get optimum visualization of Dynamics 365 CRM data with &#8211;<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lYvozZ\" target=\"_blank\" rel=\"noopener noreferrer\">Kanban Board<\/a> <\/strong>\u2013 Visualize Dynamics 365 CRM data in Kanban view by categorizing entity records in lanes and rows as per their status, priority, etc.<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lCSBaA\" target=\"_blank\" rel=\"noopener noreferrer\">Map My Relationships<\/a><\/strong> \u2013 Map My Relationships \u2013 Visualize connections and relationships between Dynamics 365 CRM entities or related records in a Mind Map view.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Microsoft Dynamics 365 July Update, formally known as V9.0 has come up with lots of new and exciting features. No doubt these features have been applauded by end users and developers. One of the important enhancement that will be boon for developer is Xrm.WebApi. Earlier to v9.0, to perform CRUD operation in Dynamics CRM\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2018\/01\/dynamics-365-v9-0-xrm-webapi-crud-operations-part-1\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":11130,"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":[18,65],"tags":[],"class_list":["post-11129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365-v9-2","category-webapi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/11129","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=11129"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/11129\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/11130"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=11129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=11129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=11129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}