How to create custom action in Dataverse

In this post we will check how to create an custom action, which can be triggered by any entity and run a custom business logic.

What is a Custom Action

A custom action is a process which category is action. This action can be triggered with a custom message, can be independent from entities and can be run a custom business logic written in C# like plugins.

Creating Custom Action

Let’s see how to create a custom action through a simple example project. The business logic is as follows:

  1. We need an input parameter which can be any text.
  2. Our action will convert it to uppercase
  3. And the converted text will be returned as an output parameter.

Unlike the plugins, we do not code and register immediately, because the first step is to create an action typed process in Dataverse.

Let’s see this step-by-step on the maker portal:

  1. Create or select an existing solution,
  2. then select action through “New > Automation > Process > Action”:

Create a new action in solution
Creating an Action from the solution

An action creation blade will be come in on the left, where we can provide:

  1. a name, which will be used for the custom message
  2. select “None” under option “Add to” which means it will be unbound to any entities, so it will be globally available.

Creating new action
Creating new Action

Save it, and let’s see our new process:

The new action
The recently created action

Note that the name of the process is created from the “Unique name” and the publisher prefix.

  • The type of “Category” is “Action”.
  • Scrolling down downwards we can define the input and output paremeters. The type of parameters must be provided and they can be optional or required.

In our case the input parameter will be a text and the output parameter which will be the camel case text:

Parameters
Parameters

Keep in mind that name of the parameters will be used in the code. Now we need to activate the action by clicking on the button “Activate” located on the ribbon:

Activating the process

Note: The popup dialog for confirming activation can be very slow, but if you try to turn it on in the solution itself, it is much quicker.

Business Logic

Let’s see how to write and compile our business logic in Visual Studio. First we need a project, see my previous post for further details. Now we can focus only on the code:

using Microsoft.Xrm.Sdk;
using System;

namespace kogerohu.Actions
{
    public class TextToUpperCase : IPlugin
    {
        /// <summary>
        /// Action to convert an input text to upper case and return
        /// </summary>
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            var inputText = (string)executionContext.InputParameters["inputText"];

            if (string.IsNullOrWhiteSpace(inputText)) return;

            var outputText = inputText.ToUpper();

            executionContext.OutputParameters["outputText"] = outputText;
        }
    }
}

After building our code, uploade the assembly according to the method presented in this previous post. Important difference is that when you set the “Message”, then you need to provide the unique name of the process itself. We do not have to set a primary entity, because we defined our action as global (unbound). Also do not forget to set the stage as post operation:

Update existing step

That’s it, our plugin typed custom action is ready to use. Now it will be triggered and we can also call it from JavaScript, as workflow step or in Power Automate as unbound action.

The source code with the VS project is available on my GitHub page.

Source

  1. https://learn.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/create-own-actions?view=op-9-1

Leave a Reply

Your email address will not be published. Required fields are marked *