Custom Actions

How to dev and test Custom Actions

You can use either early-bound custom actions or late bound custom actions. If you use early-bound custom actions you’ll have a subclass of an OrganizationRequest class that you’ll use to trigger the execution of said custom action. Whereas with late bound, you’ll use the OrganizationRequest with the RequestName property.

You can find more information about how to generate early-bound classes for custom actions in this post: You’ll find more info on the Microsoft learn site, under the Using Custom Actions section.

In FakeXrmEasy, we can use FakeMessageExecutors to simulate the response of these custom actions:

  • If the custom action is an early-bound custom action, you can use the FakeMessageExecutor class and the AddFakeMessageExecutors method in middleware to use them.

  • If the custom action is a late-bound custom action, you can use the GenericFakeMessageExecutor class and the AddGenericFakeMessageExecutors method in middleware to use them.

Early-bound custom actions

When developing and testing code that depends on early-bound custom actions, you would:

  1. Define a FakeMessageExecutor class to execute the early-bound custom action so it will return a simulated response.
  2. Enable FakeMessageExecutors in the middleware setup
  3. Once FakeMessageExecutors are enabled, you just need to repeat Step 1 for any other early-bound action you want to test.

Create a FakeMessageExecutor for the action

This example assumes you have an action generated as new_TestActionRequest, and that it returns a new_TestActionResponse.

public class TestActionExecutor : IFakeMessageExecutor
{
    public bool CanExecute(OrganizationRequest request)
    {
        return request is new_TestActionRequest;
    }

    public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx)
    {
        var response = new new_TestActionResponse();
        response["output"] = request["input"];
        return response;
    }

    public Type GetResponsibleRequestType()
    {
        return typeof(new_TestActionRequest);
    }
}
var context = MiddlewareBuilder
        .New()

        .AddCrud()
        .AddFakeMessageExecutors(Assembly.GetAssembly(typeof(new_TestActionRequest)))

        .UseCrud()
        .UseMessages()
        
        .SetLicense(FakeXrmEasyLicense.RPL_1_5)
        .Build();

Late-bound custom actions

This section will guide about developing and mocking late-bound custom actions. Similarly to early-bound custom actions, you would:

  1. Define a class to execute the late-bound custom action so it will return a simulated response.
  2. Enable GenericFakeMessageExecutors in the middleware setup
  3. Once GenericFakeMessageExecutors are enabled, you just need to repeat Step 1 for each late-bound action you want to test.

Create a GenericFakeMessageExecutor

Here we are using a demo action name for testing purposes. Let’s assume we have a custom action with a “input” property and an “output” property. The custom action simply This GenericMessageExecutor will be executed for a “new_TestAction” request, and it will return a property with name “output” that matches what it received as the “input”.

public class NewGenericFakeMessageExecutor : IGenericFakeMessageExecutor
{
    private const string RequestName = "new_TestAction";

    public bool CanExecute(OrganizationRequest request)
    {
        return request.RequestName == RequestName;
    }

    public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx)
    {
        OrganizationResponse response = new OrganizationResponse();
        response["output"] = request["input"];
        return response;
    }

    public Type GetResponsibleRequestType()
    {
        return typeof(OrganizationRequest);
    }

    public string GetRequestName() 
    {
        return RequestName;
    }
}

Repeat this step for each late bound custom action.

Add the method AddGenericFakeMessageExecutors in middleware

Then, during the middleware setup, add a AddGenericFakeMessageExecutors where you pass the Assembly where one or more of the generic fake message executors are located. You only need to do this once per assembly.

var context = MiddlewareBuilder
        .New()
        .AddCrud()
        .AddGenericFakeMessageExecutors(Assembly.GetAssembly(typeof(NewGenericFakeMessageExecutor)))
        .UseCrud()
        .UseMessages()
        .SetLicense(FakeXrmEasyLicense.RPL_1_5)
        .Build();