Creating Custom Connector In Power Automate

Custom connectors provide a way to communicate with services that are not available as pre-built connectors.
Like any other power automate trigger and actions custom connectors can also have their actions and triggers.

For the process of building a custom connector, let's create an Azure function that executes the calculate rollup field request in Dataverse.
Of course, we could simply execute the request in Dataverse webapi and call it as part of flow or plugin but to build the custom connector, I will have the logic inside an azure function.

Some Interesting articles, around the possibilities and issues of having the 'CalculateRollupField' Request in custom connector

https://silicium-consulting.com/tutorial-automatically-calculate-rollup-fields-using-power-automate/

https://powerusers.microsoft.com/t5/Building-Power-Apps/Custom-Connector-Invalid-expression-for-Parameter/td-p/587563

Build Azure Function to Calculate Roll-up Field

The process of creating, and publishing an azure function can be seen here
Azure function details, to simply I will just be sharing the code for this scenario so that the focus can be on the custom connector part.

namespace FunctionCalculateRollup
{

    public class CalculateRollupField
    {

        [FunctionName("CalculateRollupField")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string connectionString = Environment.GetEnvironmentVariable("DataVerseConnection");
            if (string.IsNullOrEmpty(connectionString))
            {
                return new StatusCodeResult(StatusCodes.Status500InternalServerError);
            }

            string entityName = req.Query["entityName"];
            string id = req.Query["id"];
            string fieldName = req.Query["fieldName"];

            if (string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(id) || string.IsNullOrEmpty(fieldName))
            {
                return new BadRequestObjectResult("Please provide valid entityName, id, and fieldName parameters.");
            }

            try
            {
                ServiceClient service = new ServiceClient(connectionString);


                if (service.IsReady)
                {
                    // Create the CalculateRollupRequest
                    CalculateRollupFieldRequest rollupRequest = new CalculateRollupFieldRequest
                    {
                        Target = new EntityReference(entityName, new Guid(id)),
                        FieldName = fieldName
                    };

                    CalculateRollupFieldResponse response = (CalculateRollupFieldResponse)await service.ExecuteAsync(rollupRequest);

                    // Handle the response as needed
                    return new OkObjectResult("Rollup calculation executed successfully.");
                }
                else
                {
                    return new StatusCodeResult(StatusCodes.Status500InternalServerError);
                }
            }
            catch (Exception ex)
            {
                return new ObjectResult($"An error occurred: {ex.Message}") { StatusCode = StatusCodes.Status500InternalServerError };
            }
        }
    }
}

local.settings.json

Testing the Azure function in Postman

The code can be found in the azure portal.
Go to the azure function app and select the azure function and click on Function Keys
and copy, alternatively on the function you can click on Get Function Url

Now, that the azure function has been tested and executed successfully, let's consume the function, to do so, I will be creating a custom connector, that can be used as part of power automate cloud flows, this custom connector is generic and works across the entities that would need instant roll-up calculations.

**Creating custom connector in Power Automate.**
Navigate to your maker portal, https://make.powerapps.com select a solution and choose custom connector option and click to create one.

In the next screen give the host name and base URL, host name is something that the function URL will have

In the security tab, choose no authentication

Next, In the definition tab add the action

Next under the Request click + Import from the sample

Select the verb and add the function URL without the code part, something like the one below in my case.

https://functiondataversequeues.azurewebsites.net/api/CalculateRollupField?entityName=account&id=a6aaf6b8-5938-ee11-bdf4-000d3af06307&fieldName=new_nooftasks

The code part is the function key that we saw above.
Scroll down to ensure validation is succeeded

In the same screen, click on the policy and add the API key

After which, click on create connector, move on to the code tab and then to Test tab and click on the new connection

Pass the parameters and test operation

The custom connector is now able to successfully execute the azure function

Using the custom connector in Power Automate

Go back to the solution, create a cloud flow and in this case, for the sake of this example, I choose a manual trigger, and in the next step choose an operation, and you should notice the custom connector that was built

This way the custom connector can be used and, with passing the right parameters this will calculate the roll-up field for an entity when the flow triggers.