{"id":2236,"date":"2016-04-15T18:58:40","date_gmt":"2016-04-15T18:58:40","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2236"},"modified":"2016-04-15T19:15:34","modified_gmt":"2016-04-15T19:15:34","slug":"calling-custom-actions-using-web-api","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2236","title":{"rendered":"Calling Custom Actions using Web API"},"content":{"rendered":"<p>In our last web API articles, we discussed about using <a href=\"https:\/\/himbap.com\/blog\/?p=2195\">web API actions<\/a>, today we are going to discuss about calling our custom actions using web API. If you are new to action you can refer <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dn481600.aspx\">this KB<\/a> to know about actions.<br \/>\nLet\u2019s take a scenario that we want to design one action for our custom event entity to approve event based on it\u2019s budget field value, for example if budget is more than 50000 then it will be approved otherwise not. This action will have only one output parameter, which will return true or false based on\u00a0event is approved or not. So let\u2019s design our action first, using following steps:<br \/>\n1. Navigate to <strong>Settings-&gt;Customizations-&gt;Customize the system<\/strong>.<br \/>\n2. Navigate to <strong>Components-&gt;Entities-&gt;New<\/strong> under solution and create a custom Event entity with two below custom attributes:<br \/>\n<strong>Name<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>Data Type<\/strong><br \/>\nEvent Approved\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Two Options<br \/>\nEvent Budget\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0Currency<br \/>\n3. Place these field over <strong>Main<\/strong> form and <strong>Publish<\/strong> your customization.<br \/>\n4. Navigate to <strong>Settings-&gt;Processes-&gt;New<\/strong> and use following details and click on Ok.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/budgetapprovalaction.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2239 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/budgetapprovalaction-300x87.jpg\" alt=\"budgetapprovalaction\" width=\"300\" height=\"87\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/budgetapprovalaction-300x87.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/budgetapprovalaction-624x180.jpg 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/budgetapprovalaction.jpg 665w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n5. Setup an output argument using following details:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/outputparameter1.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2240 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/outputparameter1-300x79.jpg\" alt=\"outputparameter\" width=\"300\" height=\"79\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/outputparameter1-300x79.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/outputparameter1-624x164.jpg 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/outputparameter1.jpg 845w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n6. Click on <strong>Add Step<\/strong> drop down button and add a <strong>Check Condition<\/strong> step.<br \/>\n7. Configure this steps using following condition and click on <strong>Save and Close<\/strong> button:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/checkcondition.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2243 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/checkcondition-300x67.jpg\" alt=\"checkcondition\" width=\"300\" height=\"67\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/checkcondition-300x67.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/checkcondition.jpg 618w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n8. Click on the row immediate after <strong>Check Condition<\/strong> and add an update step.<br \/>\n9. Click on <strong>Set Properties<\/strong> button and set <strong>Event Approval<\/strong> to <strong>Yes<\/strong>, like below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/eventapproved1.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2244 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/eventapproved1-300x34.jpg\" alt=\"eventapproved\" width=\"300\" height=\"34\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/eventapproved1-300x34.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/eventapproved1-624x70.jpg 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/eventapproved1.jpg 756w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n10. Now click on if condition and add <strong>Default<\/strong> step from <strong>Add Step<\/strong> drop down.<br \/>\n11. Similarly add an update steps immediate after <strong>Otherwise<\/strong> setp and this time set <strong>Event Approved<\/strong> to false using <strong>Set Properties<\/strong> button.<br \/>\n12. Now add a <strong>Assign Value<\/strong> step outside of if condition scope and assign <strong>Event Approved<\/strong> field value to output argument like following screenshot:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/assignvalue.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2238 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/assignvalue-300x210.jpg\" alt=\"assignvalue\" width=\"300\" height=\"210\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/assignvalue-300x210.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/assignvalue-624x437.jpg 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/assignvalue.jpg 675w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n13. Finally action steps should be similar to following screenshot, now activate action using <strong>Activate<\/strong> button on toolbar.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/fullaction1.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2245 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/fullaction1-300x190.jpg\" alt=\"fullaction\" width=\"300\" height=\"190\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/fullaction1-300x190.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/fullaction1-624x395.jpg 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/04\/fullaction1.jpg 769w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now our action is ready, just make sure to note down name of the action and output argument we during Web API call. As we don&#8217;t have any input argument that we need to pass, so we can simply pass our event id and call our custom action using it&#8217;s name like below. After that we can capture output argument under data collection.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction Callcustomaction() {\r\n    var Id = Xrm.Page.data.entity.getId().substring(1, 37);\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;POST&quot;, serverURL + &quot;\/api\/data\/v8.0\/dev_events(&quot; + Id + &quot;)\/Microsoft.Dynamics.CRM.new_CheckforBudgetApproval&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                var data = JSON.parse(this.response);\r\n                if (data['Approved'] != null)\r\n                    alert(data['Approved']);\r\n            } else {\r\n                var error = JSON.parse(this.response).error;\r\n                alert(error.message);\r\n            }\r\n        }\r\n    };\r\n    req.send();\r\n}\r\n<\/pre>\n<p>We can use above code in script web resource and can call <strong>Callcustomaction<\/strong> method on some ribbon button or some form\/or field event to test it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last web API articles, we discussed about using web API actions, today we are going to discuss about calling our custom actions using web API. If you are new to action you can refer this KB to know about actions. Let\u2019s take a scenario that we want to design one action for our custom event entity to approve&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2236\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,275,296],"tags":[355,354,281],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2236"}],"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=2236"}],"version-history":[{"count":8,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2236\/revisions"}],"predecessor-version":[{"id":2253,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2236\/revisions\/2253"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}