Trigger HTTP Flows From Dataverse Using Environment Variables And The Modern Command Bar

Triggering HTTP flows from a Dataverse button shouldn’t require hardcoded URLs, messy JavaScript, or digging through the old ribbon editor. You might have used them in the past where we had no choice but to hard code them on a button, and then later on by using Scott Durow‘s wonderful Smart Buttons and passing the HTTP URL through from an environment variable. With the modern command designer, environment variables, and a simple Run JavaScript action, you can trigger any HTTP flow cleanly and keep your solution maintainable without having to use the Ribbon Workbench any longer (RIP). In this walkthrough, I’ll show you how to use a web resource script, then add a command bar button that reads a URL from an environment variable, sends the record ID to a Power Automate flow, and does it all using a repeatable parameter-driven approach. It’s a future-proof pattern that keeps your commands portable, secure, and easy to update.

First, a couple of reminders on the flows that use an HTTP Trigger, in that if you are not already on top of this, you might find that some of your flows are no longer firing. Read this post from me about how to find the flows that use an HTTP Trigger and their respective Environment variables, and then this post from Jeroen Scheper where he highlights an issue he had recently where the length of the URL was a bit strange. OK first things first, we need a web resource that holds a script which we will use to run when a button is pressed. Create your web resource and paste in the script (shared below), give it a name and put in a description. Make sure you put instructions for others on how to use it.

Click to view in detail

Special thanks to my brother who helped me out understanding how we can do a repeatable script (rather than needing a web resource for each button) but in true style said he did nothing. 😉I was then able to figure out I can pass the parameters through on each command as needed. The script has two main parts to it which happen when someone presses a button on a record.

1. getEnvironmentVariable(schemaName)

  • Purpose: Retrieve the value of an environment variable in Dynamics 365 based on its schema name.
  • How it works:
    • Builds a FetchXML query to look for the environment variable with the given schema name.
    • Ensures only active variables are retrieved (statecode = 0).
    • Uses Xrm.WebApi.retrieveMultipleRecords to query the environment variable table.
    • If the variable isn’t found, or has no value, it throws an error.

2. getAction(primaryControl, envVarName, customMessage)

  • Purpose: Trigger a flow URL fetched from the environment variable, while managing notifications to the user on the form.
  • Step-by-step:
    1. Show immediate notification: Displays a warning on the form (customMessage) so the user knows something is happening.
    2. Retrieve environment variable: Calls getEnvironmentVariable to get the flow URL.
    3. Trigger the flow: Uses fetch to POST to the flow URL.
    4. Handle errors: If the environment variable is missing, has no value, or the HTTP call fails, it replaces the warning with an error message on the form.

Note that I have used the WARNING notification but more so for the yellow colour background. INFO used to be perfect until the colour background was changed, and now it is just a boring grey that can be missed easily. So certainly you can swap that for INFO instead should you choose.

/*** Fetch an Environment Variable value using its Schema Name. */

function getEnvironmentVariable(schemaName) {

    var fetchXml = [
        "<fetch version='1.0' mapping='logical' distinct="true">",
        "  <entity name="environmentvariablevalue">",
        "    <attribute name="value"/>",
        "    <link-entity name="environmentvariabledefinition" link-type="inner" ",
        "         from='environmentvariabledefinitionid' to='environmentvariabledefinitionid'>",
        "      <filter type="and">",
        "        <condition attribute="schemaname" operator="eq" value="", schemaName, ""/>",
        "      </filter>",
        "    </link-entity>",
        "    <filter type="and">",
        "      <condition attribute="statecode" operator="eq" value="0"/>",
        "    </filter>",
        "  </entity>",
        "</fetch>"
    ].join("");

    return Xrm.WebApi.retrieveMultipleRecords(
        "environmentvariablevalue",
        "?fetchXml=" + encodeURIComponent(fetchXml)
    )
    .then(function (result) {
        if (!result.entities || result.entities.length === 0) {
            throw new Error("Environment variable not found: " + schemaName);
        }

        var value = result.entities[0].value;

        if (!value) {
            throw new Error("Environment variable '" + schemaName + "' has no value.");
        }

        return value;
    });
}

/*** Pass through parameters and run function for the flow. */

function getAction(primaryControl, envVarName, customMessage) {
    const formContext = primaryControl;

    // Show the custom WARNING message immediately
    formContext.ui.setFormNotification(customMessage, "WARNING", "customHttpMessage");

    // Fetch the environment variable
    getEnvironmentVariable(envVarName)
        .then(function(flowUrl) {
            if (!flowUrl) {
                throw new Error("Environment variable " + envVarName + " returned no value");
            }

            // Call the flow
            return fetch(flowUrl, { method: "POST" });
        })
        .then(function(response) {
            if (!response.ok) {
                throw new Error("HTTP call failed with status " + response.status);
            }
        
        })
        .catch(function(error) {
            // Replace the custom message with the error
            formContext.ui.clearFormNotification("customHttpMessage");
            formContext.ui.setFormNotification(error.message, "ERROR", "customHttpMessage");
        });
}

Once you’ve got your web resource sorted, we can use it on a button. More on how to do that in a moment. Before doing that, you will need to make sure you have the information about where the URL for your flow is stored. It’s any flow that has this as the starting trigger, when an HTTP request is received. We need the HTTP POST URL from each of those flows.

Click to view in detail

Then you will need a corresponding Environment Variable for each flow. Add in the URL from the flow, and make sure you get the Name of the Environment Variable too as we will need that for the button.

Click to view in detail

Now go in to the table command or command on one of your model-driven apps and add a new command. If you are not sure how to get to this point, make sure you check out this post where it is explained in more detail.

Click to view in detail

For each button where you want to start a flow that has an HTTP trigger, you will use the same web resource for the Run JavaScript action, where the function name needs to be getAction.

Click to view in detail

Then each button needs 3 parameters which MUST BE in this order. Set Parameter 1 as the primary control. Parameter 2 is a string and should be the name of your Environment Variable. Parameter 3 is also a string and should be the message you want to display to the user after pressing the button.

  • primaryControl → the form context.
  • envVarName → the schema name of the environment variable containing the flow URL.
  • customMessage → a message shown immediately on the form.
Click to view in detail

Now here it is in action. One ribbon for the Opportunity, with three buttons. Flow One button is pressed which runs the corresponding flow and shows a custom message.

Click to view in detail

Flow Two button is pressed which runs the corresponding flow and a different custom message.

Click to view in detail

Finally, Flow Three button is pressed which runs the corresponding flow with another different custom message. Hooray! Now when you move the table through to other environments in solutions, you only need to update the environment variables with the new URL for the HTTP Flows rather than trying to edit anything hard coded in to the ribbon.

Click to view in detail

Original Post http://meganvwalker.com/trigger-http-flows-from-dataverse-modern-command/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Follow
Search
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...