{"id":37343,"date":"2024-02-05T17:41:33","date_gmt":"2024-02-05T12:11:33","guid":{"rendered":"https:\/\/techmasala.addastudents.com\/dev\/?p=37343"},"modified":"2024-02-09T22:51:52","modified_gmt":"2024-02-09T17:21:52","slug":"how-to-call-custom-action-from-javascript-in-dynamics-365-using-xrm-webapi-online-execute","status":"publish","type":"post","link":"https:\/\/techmasala.addastudents.com\/dev\/how-to-call-custom-action-from-javascript-in-dynamics-365-using-xrm-webapi-online-execute\/","title":{"rendered":"How to Call Custom Action from JavaScript in Dynamics 365 using Xrm.WebApi.online.execute"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Custom actions in Microsoft Dynamics 365 (or Power Apps) allow you to encapsulate business logic and execute it from various contexts, such as entity forms, workflows, or custom code. In this article, we\u2019ll explore how to call a custom action using JavaScript and the&nbsp;<code>Xrm.WebApi.online.execute<\/code>&nbsp;method.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Prerequisites<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Before we dive into the implementation, make sure you have the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Dynamics 365 Solution<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Create or open a solution in your Dynamics 365 environment where you want to define the custom action.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Custom Action<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Create a custom action with input and output parameters. Define the logic you want to execute when the action is called.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"957\" height=\"543\" src=\"https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2024\/02\/image.png\" alt=\"\" class=\"wp-image-37347\" srcset=\"https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2024\/02\/image.png 957w, https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2024\/02\/image-300x170.png 300w, https:\/\/techmasala.addastudents.com\/dev\/wp-content\/uploads\/2024\/02\/image-768x436.png 768w\" sizes=\"auto, (max-width: 957px) 100vw, 957px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong><br>Steps to Call a Custom Action from JavaScript:<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Configure Custom Action in Power App Solution:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open your Power Apps solution.<\/li>\n\n\n\n<li>Add a custom action process with input and output parameters (both string types).<\/li>\n\n\n\n<li>Activate the custom action.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. Create a JavaScript Web Resource:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Develop a JavaScript web resource.<\/li>\n\n\n\n<li>Use the following code snippet and publish it:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ <strong>Call Global Custom Action from JavaScript using Web API<\/strong>\n\nfunction runCustomAction() {\n    \/\/ Get the current record's ID\n    var entityId = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');\n\n    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\/\/ Define the target entity for the custom action<\/mark>\n<mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">    var targetEntity = {\n        entityType: \"custom_entity\", \/\/ Replace with the actual entity logical name\n        id: entityId\n    };<\/mark>\n\n    \/\/ Define the 'From' user\n    var fromUser = {\n        entityType: \"systemuser\",\n        id: \"9C9CA531-E021-E911-A9BC-000D3A1B913D\" \/\/ Replace with the actual user ID\n    };\n\n    \/\/ Define the 'To' user\n    var toUser = {\n        entityType: \"systemuser\",\n        id: \"6E8B062B-E021-E911-A9BC-000D3A1B913D\" \/\/ Replace with the actual user ID\n    };\n\n    \/\/ Create a request object with input parameters for the custom action\n    var request = {\n        FromEmail: fromUser,\n        ToEmail: toUser,\n        <mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">entity: targetEntity<\/mark>,\n        getMetadata: function () {\n            return {\n                <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">boundParameter<\/mark>: <mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">\"entity\"<\/mark>,\n                parameterTypes: {\n                    \"<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">entity<\/mark>\": <mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">{\n                        typeName: \"mscrm.custom_entity\", \/\/ Replace with the actual entity logical name\n                        structuralProperty: 5\n                    }<\/mark>,\n                    \"FromEmail\": {\n                        \"typeName\": \"mscrm.systemuser\",\n                        \"structuralProperty\": 5\n                    },\n                    \"ToEmail\": {\n                        \"typeName\": \"mscrm.systemuser\",\n                        \"structuralProperty\": 5\n                    }\n                },\n                operationType: 0, \/\/ 0 for Action\n                operationName: \"new_RouteBugtoDeveloper\" \/\/ Replace with the actual custom action name\n            };\n        }\n    };\n\n    \/\/ Execute the custom action using Xrm.WebApi.online.execute\n    Xrm.WebApi.online.execute(request).then(\n        function (data) {\n\n          <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">  <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\/\/ Handle success response<\/mark>\n           <mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\"> data.json().then(\n                function (output) {\n                    var outputval = output.your_outputparameter_name;\n                    \/\/ Do something with the output parameter\n                }\n            );<\/mark>\n\n           \n        },\n        function (error) {\n            \/\/ Handle error response\n          \n            var errorMessage = error.message;\n        }\n    );\n}\n\n\/\/ Example: Call the custom action\nrunCustomAction();\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">Below are the changes you need to make to the global custom action.<\/mark><br><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Removal of Entity Specification:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The line <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">entity: targetEntity<\/mark><\/code> has been removed from the request object. This signifies that the action no longer targets a specific entity instance, making it applicable globally.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Setting <mark style=\"background-color:#ffffff\" class=\"has-inline-color\">BoundParameter<\/mark> to Null:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">boundParameter<\/mark><\/code> is explicitly set to <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">null<\/mark><\/code>. This adjustment indicates that the action is not bound to any particular entity, emphasizing its global scope and applicability across the system.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Removed the Entity from parameter types:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The <mark style=\"background-color:#fcb900\" class=\"has-inline-color\">entity<\/mark> is removed from the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">parameterTypes<\/mark><\/code>. This change denotes that the action no longer expects entity-specific parameters, reinforcing its global nature and independence from the entity context.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">3. Add a Command Button on Entity Form:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add a command button to the entity form.<\/li>\n\n\n\n<li>Call the&nbsp;<code>CallCustomActionFromJavaScript<\/code>&nbsp;function when the button is clicked.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. Develop a Plugin:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a plugin with the following code:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing Microsoft.Xrm.Sdk;\n\nnamespace Sample_Plugin\n{\n    public class CustomActionCall : IPlugin\n    {\n        public void Execute(IServiceProvider serviceProvider)\n        {\n            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));\n            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));\n            IOrganizationService service = factory.CreateOrganizationService(context.UserId);\n\n            var MyInputParam = (string)context.InputParameters&#91;\"MyInputParam\"];\n            context.OutputParameters&#91;\"MyOutputParam\"] = \"This is the output parameter\";\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Register the plugin to execute when the custom action is called.<\/li>\n\n\n\n<li>Customize the plugin logic according to your business requirements.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Calling custom actions using Xrm.WebApi.online.execute in Dynamics 365 allows developers to extend the platform&#8217;s capabilities and execute custom server-side logic from the client side using JavaScript. This article provided a code example demonstrating how to construct and execute a custom action with input parameters. Developers can leverage this approach to enhance and customize Dynamics 365 applications based on specific business requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Custom actions in Microsoft Dynamics 365 (or Power Apps) allow you to encapsulate business logic and execute it from various contexts, such as entity forms, workflows, or custom code. In this article, we\u2019ll explore how to call a custom action using JavaScript and the&nbsp;Xrm.WebApi.online.execute&nbsp;method. Prerequisites Before we dive into the implementation, make sure you have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":37352,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":"[]"},"categories":[70,17,72],"tags":[887,885,47,886],"class_list":["post-37343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn-technology-free","category-learn-microsoft-dynamics-crm","category-tech","tag-custom-action","tag-dynamics-365","tag-javascript","tag-xrm-webapi-online-execute"],"_links":{"self":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/comments?post=37343"}],"version-history":[{"count":12,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37343\/revisions"}],"predecessor-version":[{"id":37370,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37343\/revisions\/37370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media\/37352"}],"wp:attachment":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media?parent=37343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/categories?post=37343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/tags?post=37343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}