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’ll explore how to call a custom action using JavaScript and the Xrm.WebApi.online.execute
method.
Prerequisites
Before we dive into the implementation, make sure you have the following:
- Dynamics 365 Solution:
- Create or open a solution in your Dynamics 365 environment where you want to define the custom action.
- Custom Action:
- Create a custom action with input and output parameters. Define the logic you want to execute when the action is called.
Steps to Call a Custom Action from JavaScript:
1. Configure Custom Action in Power App Solution:
- Open your Power Apps solution.
- Add a custom action process with input and output parameters (both string types).
- Activate the custom action.
2. Create a JavaScript Web Resource:
- Develop a JavaScript web resource.
- Use the following code snippet and publish it:
// Call Global Custom Action from JavaScript using Web API
function runCustomAction() {
// Get the current record's ID
var entityId = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');
// Define the target entity for the custom action
var targetEntity = {
entityType: "custom_entity", // Replace with the actual entity logical name
id: entityId
};
// Define the 'From' user
var fromUser = {
entityType: "systemuser",
id: "9C9CA531-E021-E911-A9BC-000D3A1B913D" // Replace with the actual user ID
};
// Define the 'To' user
var toUser = {
entityType: "systemuser",
id: "6E8B062B-E021-E911-A9BC-000D3A1B913D" // Replace with the actual user ID
};
// Create a request object with input parameters for the custom action
var request = {
FromEmail: fromUser,
ToEmail: toUser,
entity: targetEntity,
getMetadata: function () {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
typeName: "mscrm.custom_entity", // Replace with the actual entity logical name
structuralProperty: 5
},
"FromEmail": {
"typeName": "mscrm.systemuser",
"structuralProperty": 5
},
"ToEmail": {
"typeName": "mscrm.systemuser",
"structuralProperty": 5
}
},
operationType: 0, // 0 for Action
operationName: "new_RouteBugtoDeveloper" // Replace with the actual custom action name
};
}
};
// Execute the custom action using Xrm.WebApi.online.execute
Xrm.WebApi.online.execute(request).then(
function (data) {
// Handle success response
data.json().then(
function (output) {
var outputval = output.your_outputparameter_name;
// Do something with the output parameter
}
);
},
function (error) {
// Handle error response
var errorMessage = error.message;
}
);
}
// Example: Call the custom action
runCustomAction();
Below are the changes you need to make to the global custom action.
- Removal of Entity Specification:
- The line
entity: targetEntity
has been removed from the request object. This signifies that the action no longer targets a specific entity instance, making it applicable globally.
- The line
- Setting BoundParameter to Null:
boundParameter
is explicitly set tonull
. This adjustment indicates that the action is not bound to any particular entity, emphasizing its global scope and applicability across the system.
- Removed the Entity from parameter types:
- The entity is removed from the
parameterTypes
. This change denotes that the action no longer expects entity-specific parameters, reinforcing its global nature and independence from the entity context.
- The entity is removed from the
3. Add a Command Button on Entity Form:
- Add a command button to the entity form.
- Call the
CallCustomActionFromJavaScript
function when the button is clicked.
4. Develop a Plugin:
- Create a plugin with the following code:
using System;
using Microsoft.Xrm.Sdk;
namespace Sample_Plugin
{
public class CustomActionCall : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
var MyInputParam = (string)context.InputParameters["MyInputParam"];
context.OutputParameters["MyOutputParam"] = "This is the output parameter";
}
}
}
- Register the plugin to execute when the custom action is called.
- Customize the plugin logic according to your business requirements.
Calling custom actions using Xrm.WebApi.online.execute in Dynamics 365 allows developers to extend the platform’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.
Please write another blog with real time example such as assign records to another etc.
Thanks for the blog
sure…will try to cover this 😊✌️
First off I want to say great blog! I had a quick question in which I’d like to ask if you do not mind.
I was interested to know how you center yourself and
clear your head prior to writing. I’ve had trouble clearing my
mind in getting my thoughts out. I do take pleasure
in writing but it just seems like the first 10 to 15 minutes are wasted
simply just trying to figure out how to begin. Any recommendations or hints?
Cheers!