Amey Holden

View Original

Trigger a Power Automate Flow from a Custom Button in Dynamics 365 or Dataverse

Since the dawn of Power Automate flows, there has been a need to trigger them on demand directly from a specific row. We can add custom buttons to the command bar In model-driven apps so easily now using the ‘command designer’ and PowerFx directly in the lovely power apps maker portal. Using PowerFx is great for making updates directly to the row which the button belongs to and also related items via a lookup (N:1), but it has it’s limitations and cannot be used to call flows directly.

There are few solutions out there already for triggering a Power Automate flow from a custom buttons/commands including the below, each has its pros/cons:

  1. Triggering a flow from a HTTP request (unauthenticated) 😑

  2. Using patch to populate a hidden field which then triggers the flow to run 😖

  3. Using the chronically problematic, legacy and hidden ‘When a row is selected’ Dataverse trigger in Power Automate 🤬

I’m sure there are more ways but that’s enough to make the point that it’s already ‘possible’ but I think I found a nicer way to do it. This method will use a custom page that opens as a modal when a command button is selected. The custom page ‘OnStart’ property then triggers the flow and returns a response to the user when complete.

Create a Custom Page

You can create a Page from inside a solution or from the model-driven add editor. Either way once it is created make your you save and publish first. Its not even funny the number of times I have closed out of a page without saving and the whole thing has not even been created.

Create a Power Automate flow to run when the Custom Button is used

The act of the modal custom page being opened is enough to trigger the flow to start doing whatever magic it needs to do here. I find its easier to simply pass in the row GUID and handle the rest inside the flow to retrieve/update data. The custom page is just the messenger.

Check your flow looks something like this once you are done. Save your changes and return to your blank custom page.

Your custom page should now show the flow as available ‘in your app’. All time best meme imagery optional.

Launch a Custom Page modal from a model-driven app

Now we have a custom page we need to work out how to make the page load inside a model-driven app. I’m not going to go into too much details on this part because Diana Birkelbach has already done such a great job of explaining this part so go here and set up your custom page modal first. Come back when you get to ‘Grabbing the recordId’.

Make sure you save and publish all your changes to the command ribbon/JavaScript Web Resource/Model-driven app. If you are having problems getting your button to work, try launching your browser console (F12) and the errors in there might help.

Now is a good time to check everything is working ok and that you can launch the modal page from within your app successfully. Nothing is going to happen when the modal opens at this stage, but it will after the next part.

Run a Power Automate flow when the Custom Button is used

We are ready to set up the custom page so that the flow runs as soon as the button is pressed and the page opens.

Configure App OnStart

The most important part here is the ‘OnStart’ command of the ‘App’ custom page. Select ‘App’ from the tree view menu, and configure the OnStart function. I know there is a lot going on here, but I will try to explain below.

See this content in the original post

We are going to use variables inside the page to control show/hide of various pieces of the page. For example

  • If the flow is in progress (varInProg) show a spinner

  • Set the value of label based on what’s happening (varInProgLabel)

  • Capture the response from a flow (varFlowResponse)

  • The GUID of the row which the modal was launched from (RecordId) and the table (TableName)

You may need to update the value from ‘TriggeraPowerAutomateFlowfromaCustomButtoninDynamics365orDataverse.Run’ to reflect the name of the flow you created here. And also if you are not using the Account table, the LookUp() formula will need to be updated too.

Capture the flow response values

To build out the bare bones of this page we just need to a way to check the response values are coming back after the flow has run successfully.

Add two buttons which will take the inputs from the flow response for ‘DocumentURL’ and ‘FolderURL’. Update the properties:

Name: BtnOpenPDF
OnSelect: Launch(varFlowResponse.documenturl)

Name: BtnOpenSPFolder
OnSelect: Launch(varFlowResponse.folderurl)

Add a label and set the ‘Text’ property to varInProgLabel

The varInProgLabel is set from the flow response ‘Message’ and shows up in the label.

Test the page

You can Save & Publish then launch the modal from your model driven app to try this out, or you can use the //Testing stand alone section of your OnStart code to test directly in the custom page editor. You just need to remove the // comment lines for Set(Record) and Set(TableName). Provide a valid GUID for RecordId, then Run OnStart.

This should run your flow and your responses should come back to the buttons and label you created above. You can also check in your flow run history to see if that ran ok too.

You should be able to see the label return your ‘Message’ text from the flow response, you can click the buttons to check the URLs provided are there, or click on the OnSelect code and see what values are behind the buttons.

Remember - if you want to go and test the page within your modal driven app you need to comment out the testing code before you publish otherwise it will only every run with the hard coded test GUID no matter where you click the button from!

So if you want just the bare bones - your work here is done. The command bar button is launching the custom page modal, running the flow and responding back when complete. If you want to do some UI enhancements to show things like a loading spinner, show the buttons only when the links are ready and create dynamic messaging for a more informative user experience then carry on!

Create a Better User Experience

Control your buttons

In the majority of cases call a flow on demand means the user is probably expecting some kind of output or response. In this case, a link to a document and a folder which we added earlier as two buttons ‘BtnOpenPDF‘ and ‘BtnOpenSPFolder’, but they should not be available to the user if there is no link to click, update the properties below:

Name: BtnOpenPDF
OnSelect: Launch(varFlowResponse.documenturl)
Visible: And(varComplete,!IsBlank(varFlowResponse))
DisplayMode: If(Or(IsBlank(varFlowResponse.documenturl)),DisplayMode.Disabled,DisplayMode.Edit)

Name: BtnOpenSPFolder
OnSelect: Launch(varFlowResponse.folderurl)
Visible: And(varComplete,!IsBlank(varFlowResponse))
DisplayMode: If(Or(IsBlank(varFlowResponse.documenturl)),DisplayMode.Disabled,DisplayMode.Edit)

Hiding command buttons is offensive UX

You can show/hide command buttons based on row conditions with PowerFX ‘Visibility’ section of the command editor, however this is a pet peeve of mine. Hidden buttons are so confusing, there is no explanation to why the button is missing, it’s just gone - yuck! Instead lets tell the user why the button is not available, knowledge is power! In the section ‘Configure App OnStart’ we added a check condition, what this does is to check various bits of data to decide if the flow should run. This one is quite simple to just check if the account is Active/Inactive and if it’s a deactivated row - we show a very clear message to explain.

The cool thing is you can add multiple conditions/checks/messages here with a great level of complexity too. You can check data in related records and any other data source you might have using the full library of PowerFx commands. Nice!

That’s a wrap

Inspiration GIFS are all I have left. I love custom pages!