{"id":11138,"date":"2018-02-05T15:17:00","date_gmt":"2018-02-05T09:47:00","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=11138"},"modified":"2022-07-01T12:46:54","modified_gmt":"2022-07-01T07:16:54","slug":"dynamics-365-v9-0-xrm-webapi-operations-part-2","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2018\/02\/dynamics-365-v9-0-xrm-webapi-operations-part-2\/","title":{"rendered":"Dynamics 365 v9.0: Xrm.WebApi \u2013 Operations Part \u2013 2"},"content":{"rendered":"<h2>Introduction:<\/h2>\n<p>In our previous <a title=\"Dynamics 365 v9.0: Xrm.WebApi \u2013 CRUD Operations Part \u2013 1\" href=\"https:\/\/www.inogic.com\/blog\/2018\/01\/dynamics-365-v9-0-xrm-webapi-crud-operations-part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">blog<\/a>, we have discussed about performing CURD operations Create, Update, Delete and Retrieve using Xrm.WebApi. In this blog, we will discuss <strong>Retrieve Multiple<\/strong> and <strong>Execute Actions<\/strong>.<\/p>\n<p>Let\u2019s explore about these operations one by one.<\/p>\n<p><strong>1. Retrieve Multiple:<\/strong><strong>\u00a0<\/strong><\/p>\n<p>Retrieve multiple is used to retrieve more than one record from MS CRM. Suppose we want to retrieve all active contact from CRM then we will use the Retrieve multiple.<\/p>\n<p>If we want to retrieve multiple records then use<strong> Xrm.WebApi.retrieveMultipleRecords<\/strong><\/p>\n<p>The parameters of this function are shown below.<\/p>\n<ol>\n<li>Entity logical name<\/li>\n<li>Odata Query (i.e. select and filter)<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true \">\/\/this function is used to retrieve Account\r\nfunction retrieveMultipleAccounts() {\r\n    var queryOption = \"\";\r\n    try {\r\n\r\n        \/\/create the query\r\n        queryOption = \"?$select=fullname,telephone1,preferredcontactmethodcode,createdon,_parentcustomerid_value,creditlimit&amp;$filter=(statecode eq 0)\";\r\n\r\n        \/\/execute the query and get the results\r\n        Xrm.WebApi.retrieveMultipleRecords(\"contact\", queryOption)\r\n           .then(function (data) {\r\n               retrieveContactSuccess(data.entities);\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<p>In <strong>retrieveContactSucess<\/strong> function loop through the records and get the values as shown below.<\/p>\n<pre class=\"lang:default decode:true \">\/\/\/retrieve success\r\nfunction retrieveContactSuccess(results) {\r\n\r\n    try {\r\n        \/\/get the values \r\n        $.each(results, function (index, data) {\r\n\r\n            \/\/string\r\n            var fullname = data[\"fullname\"];\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<p>If you have more than 5000 records than used <strong>data.nextLink. <\/strong>And if the response has value <strong>data.nextLink<\/strong> then used it to retrieve next set of records.<\/p>\n<p><strong>2. Execute Action:<\/strong><strong>\u00a0<\/strong><\/p>\n<p>There are predefined Actions defined in Dynamics CRM 2016 that can be executed using Web API. On the following link will find the list of Web API actions. <a href=\"https:\/\/msdn.microsoft.com\/en-in\/library\/mt607829.aspx%20\">https:\/\/msdn.microsoft.com\/en-in\/library\/mt607829.aspx<\/a><\/p>\n<p>There are three types of Action as listed below.<\/p>\n<p><strong>Unbound actions<\/strong>: It is not bound to any specific entity. i.e the first parameter to such actions does not include the entity. E.g. WinOpportunity, CancelContract etc<\/p>\n<p><strong>Bound actions<\/strong>: It is bound to a specific entity. i.e the first parameter to such actions does not include the entity. e.g. \u2013 AddToQueue Action<\/p>\n<p><strong>Custom action<\/strong>: It is custom actions developed by developers using Processes.<\/p>\n<p>Now we will discuss about Qualify request. Suppose you want to qualify request programmatically then we will use Qualify request. Web API provides QualifyLead Action.<\/p>\n<p>In our <a href=\"https:\/\/www.inogic.com\/blog\/2016\/11\/qualify-lead-using-web-api-in-dynamics-365\/\">blog<\/a>, we have explained QualifyLead using WebApi. Now we will see how we can do the same using inbuilt <strong>Xrm.WebApi.<\/strong><\/p>\n<p>To execute action use<strong> Xrm.WebApi.execute<\/strong>.<\/p>\n<pre class=\"lang:default decode:true\">\/\/This function is used to qualify lead\r\nfunction qualifyLead() {\r\n    var saleOrderID = null;\r\n    var columnsSet = null;\r\n    var salesOrderToInvoiceReq = null;\r\n    var leadId = \"E5975EA3-531C-E511-80D8-3863BB3CE2C8\";\r\n    var qualifyLeadReq = \"\";\r\n    try {\r\n\r\n        qualifyLeadReq = {\r\n            entity: {\r\n                id: leadId,\r\n                entityType: \"lead\"\r\n            },\r\n            CreateAccount: true,\r\n            CreateContact: true,\r\n            CreateOpportunity: true,\r\n            Status: 3,\r\n            getMetadata: function () {\r\n                var metadata = {\r\n                    boundParameter: \"entity\",\r\n                    parameterTypes: {\r\n                        \"entity\": {\r\n                            \"typeName\": \"Microsoft.Dynamics.CRM.lead\",\r\n                            \"structuralProperty\": 5\r\n                        },\r\n                        \"CreateAccount\": {\r\n                            \"typeName\": \"Edm.Int32\",\r\n                            \"structuralProperty\": 1\r\n                        },\r\n                        \"CreateContact\": {\r\n                            \"typeName\": \"Edm.Int32\",\r\n                            \"structuralProperty\": 1\r\n                        },\r\n                        \"CreateOpportunity\": {\r\n                            \"typeName\": \"Edm.Int32\",\r\n                            \"structuralProperty\": 1\r\n                        },\r\n                        \"Status\": {\r\n                            \"typeName\": \"Edm.Int32\",\r\n                            \"structuralProperty\": 1\r\n                        }\r\n                    },\r\n                    operationName: \"QualifyLead\",\r\n                    operationType: 0\r\n                };\r\n\r\n                return metadata;\r\n            }\r\n        };\r\n\r\n        Xrm.WebApi.execute(qualifyLeadReq)\r\n            .then(function (result) {\r\n                Xrm.Utility.alertDialog(\"Success\");\r\n            },\r\n            function (error) {\r\n                var message = error.message;\r\n                \/\/Add handling of error that occurred\r\n            });\r\n  } catch (e) {\r\nXrm.Utility.alertDialog(e.message);\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><strong>Conclusion<\/strong>:<\/p>\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: In our previous blog, we have discussed about performing CURD operations Create, Update, Delete and Retrieve using Xrm.WebApi. In this blog, we will discuss Retrieve Multiple and Execute Actions. Let\u2019s explore about these operations one by one. 1. Retrieve Multiple:\u00a0 Retrieve multiple is used to retrieve more than one record from MS CRM. Suppose\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2018\/02\/dynamics-365-v9-0-xrm-webapi-operations-part-2\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":11158,"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":[589,1860],"class_list":["post-11138","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365-v9-2","category-webapi","tag-dynamics-365-v9-0","tag-xrm-webapi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/11138","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=11138"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/11138\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/11158"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=11138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=11138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=11138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}