{"id":3387,"date":"2018-10-05T06:55:19","date_gmt":"2018-10-05T06:55:19","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=3387"},"modified":"2018-10-09T06:03:10","modified_gmt":"2018-10-09T06:03:10","slug":"executing-workflow-from-command-button-using-web-api","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=3387","title":{"rendered":"Executing workflow from command button using Web API"},"content":{"rendered":"<p><strong>Introduction<\/strong><br \/>\nIn our <a href=\"https:\/\/himbap.com\/blog\/?p=2195\">earlier sample<\/a>, we discussed how to call actions using Web API, today we are going to share sample code to execute workflow from\u00a0command button using Web API.<\/p>\n<p><strong>Requirement<\/strong><br \/>\nLet&#8217;s say we got some requirement to validate some logic on click of the\u00a0command button and need to send email using workflow. Let&#8217;s see how we can do that.<\/p>\n<p><strong>Solution<\/strong><br \/>\nFirst we need to create\u00a0 custom solution, in our solution we are going to add the workflow which we want to call using Web API, our entity (we are using Contact entity for demo) and will create a java script web resource (We will be calling this web resource from\u00a0command button).<\/p>\n<p>We have created workflow on contact entity which is set to run as <strong>On Demand<\/strong> and we can copy it&#8217;s GUID from the URL like following.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3390 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn3-300x42.png\" alt=\"ribbonbtn3\" width=\"300\" height=\"42\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn3-300x42.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn3-624x87.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn3.png 904w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now we are going use following code\u00a0in our java script web resource to call above workflow.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction ExecuteWorkflow()\r\n{\r\n    \/\/show indication to user\r\n    Xrm.Utility.showProgressIndicator(&quot;Executing Workflow....&quot;);\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n    var contactID=Xrm.Page.data.entity.getId().substring(1, 37);\r\n    \/\/workflow id that we captured above\r\n    var workflowID=&quot;E7C36B0F-DCAB-4440-9A0A-FC499A4E2A84&quot;;\r\n    \r\n     var data = {\r\n        &quot;EntityId&quot;: contactID\r\n    };\r\n \r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;POST&quot;, serverURL + &quot;\/api\/data\/v8.2\/workflows(&quot;+workflowID+&quot;)\/Microsoft.Dynamics.CRM.ExecuteWorkflow&quot;, true);\r\n    req.setRequestHeader(&quot;Accept&quot;, &quot;application\/json&quot;);\r\n    req.setRequestHeader(&quot;Content-Type&quot;, &quot;application\/json; charset=utf-8&quot;);\r\n    req.setRequestHeader(&quot;OData-MaxVersion&quot;, &quot;4.0&quot;);\r\n    req.setRequestHeader(&quot;OData-Version&quot;, &quot;4.0&quot;);\r\n    req.onreadystatechange = function() {\r\n        if (this.readyState == 4 \/* complete *\/ ) {\r\n            req.onreadystatechange = null;\r\n            if (this.status == 200) {\r\n                 Xrm.Utility.closeProgressIndicator();\r\n            } else {\r\n                var error = JSON.parse(this.response).error;\r\n                alert(error.message);\r\n                Xrm.Utility.closeProgressIndicator();\r\n            }\r\n        }\r\n    };\r\n     req.send(JSON.stringify(data));\r\n}\r\n<\/pre>\n<p>In above code we are using <a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/developer\/clientapi\/reference\/xrm-utility\/showprogressindicator\">showProgressIndicator<\/a> to display a notification to use, earlier there was no supported way to show these type of notification and we <a href=\"https:\/\/himbap.com\/blog\/?p=690\">used to write unsupported<\/a>\u00a0code to show notification message. Next we are adding parameter required for <a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/web-api\/executeworkflow?view=dynamics-ce-odata-9\">ExecuteWorkflow<\/a> action, we need to pass entity id and we\u00a0are using\u00a0workflow id in our Web API request. We need to hide notification message using <a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/developer\/clientapi\/reference\/xrm-utility\/closeprogressindicator\">closeProgressIndicator<\/a>.<\/p>\n<p>Now we can add a button on contact entity, we can add button easily using <a href=\"https:\/\/www.develop1.net\/public\/rwb\/ribbonworkbench.aspx\">RIBBON WORKBENCH<\/a>, now this tool is also available on the <a href=\"https:\/\/www.xrmtoolbox.com\/\">XrmToolBox<\/a>, you can check our <a href=\"https:\/\/himbap.com\/blog\/?p=1722\">earlier post for how to add button<\/a>.<\/p>\n<p>First we will add command that we will be using in our\u00a0command button like following, we need select our java script web resource that we have created above and name of the method in script resource.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3389 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn2-300x67.png\" alt=\"ribbonbtn2\" width=\"300\" height=\"67\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn2-300x67.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn2-1024x227.png 1024w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn2-624x138.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn2.png 1388w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>After that we need to use this command in our button like following<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3388 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn1-300x124.png\" alt=\"ribbonbtn1\" width=\"300\" height=\"124\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn1-300x124.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn1-1024x425.png 1024w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn1-624x259.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/ribbonbtn1.png 1276w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>After publishing our changes we should be able to see Call Workflow button in contact entity form and once click it should show message like following and should call workflow.<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/backbtn4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3391 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/backbtn4-300x108.png\" alt=\"backbtn4\" width=\"300\" height=\"108\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/backbtn4-300x108.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/backbtn4-1024x369.png 1024w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/backbtn4-624x225.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2018\/10\/backbtn4.png 1363w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Hope it will help someone !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In our earlier sample, we discussed how to call actions using Web API, today we are going to share sample code to execute workflow from\u00a0command button using Web API. Requirement Let&#8217;s say we got some requirement to validate some logic on click of the\u00a0command button and need to send email using workflow. Let&#8217;s see how we can do that&#8230;. <a href=\"https:\/\/himbap.com\/blog\/?p=3387\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":3036,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[402,522],"tags":[653,650,404,651,652],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3387"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3387"}],"version-history":[{"count":6,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3387\/revisions"}],"predecessor-version":[{"id":3397,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3387\/revisions\/3397"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/3036"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}