Trigger Power Automate Flow from Button in Model Driven App

I have received several emails from people that asked me if it was possible to trigger a power automate flow (or workflow) from a button inside a model-driven app using the out of the box command bar editor. The answer to this question is an indirect yes. (I will explain to you in a second why it’s an ‘indirect’ yes and not a straight up yes.) Once I confirmed this is something we can do today by using PowerFX, I was asked (again by several people) to write an article on how exactly to configure this. When I searched online, I really couldn’t immediately find anything on this topic, so reason enough for me to write this article about it! The reason I said that the answer to the question if this is possible is an ‘Indirect yes’ is because I am not calling a power automate flow directly from the button. Instead, I create a text field in the table I want to create this logic for. Then I create a button for the same table and use PowerFX to populate (partially random) data in this field. The Power Automate flow (or workflow) will be triggered of the field I mentioned earlier.

Scenario

For this example I am going to use a scenario where I want to change the status of an order in the Sales Hub model driven app. I am going to create two buttons. The first button is called ‘Put on Hold’ and I want the button to update a new column in the order table called ‘HoldButton’. The PowerFX formula that is fired when the button is clicked, will update the ‘HoldButton’ column with the word ‘Active Hold’ and add a random value, so that each time the button is clicked, a unique value will be entered in the column. When the ‘HoldButton’ gets populated with data that includes the word ‘Active’, it will launch a Power Automate flow that will set the status of the order to ‘On Hold’. The second button is called ‘Release Hold’. When this button is clicked, the PowerFX formula will update the ‘HoldButton’ column of the selected order with the word ‘Remove Hold’ and add a random value, so that each time the button is clicked, a unique value will be entered in the column. When the ‘HoldButton’ gets populated with data that includes the word ‘Remove’, it will launch the same Power Automate flow that will set the status of the order to ‘In Progress’. I also want to show a notification when the button is clicked, and I only want the button to be visible when a record is selected.

Configuration

Add the triggering column

The first thing I am going to do is add the column to the order table. You can do this by navigating to make.powerapps.com and clicking on ‘tables’ on the left side of the screen. From here you’ll have to click on the ‘All’ button in the middle of the screen, as by default only default tables will be shown in the list. Enter ‘Orders’ in the search bar on the top of the screen, then open the order table. Click ‘Columns’ below the ‘schema’ section in the middle of the screen. Click ‘+New column’ and name the column. In this example, I am calling the new column ‘HoldButton’ and the datatype is single line of text.

Make sure you save the new column and publish the table. You can publish the table by clicking the ‘Advanced’ button on the command bar after you saved your column, and then selecting ‘Publish Table’.

Add the button(s)

To add buttons to a form, main view or grid view you’ll need to access the ribbon editor. You can do this by navigating to make.powerapps.com and clicking ‘Apps’ on the left-hand side. From here you’ll need to select the model-driven app you want to add the buttons to, which in my case is the Sales Hub. When you hover your mouse next to the model-driven app, you’ll notice a pencil icon shows up, which you will need to click to enter the app editor. From here you will need to locate the table for which you want to add the button(s) to, which in my case is the order table. I type ‘Order’ in the top search bar, and then click ‘Order views’. Scroll down until you find the ‘Order Views’ and click on the tree dots on the right side of the Order views, then hover your mouse over the ‘Edit Command Bar’ and select ‘Edit’ or ‘Edit in new tab’.
On the next screen you will have to select where you want to add the buttons, on the main grid, the main form, on subgrid views, or the associated view. In this example I am going to add the buttons to the subgrid view, so I select that and then click ‘Edit’. This will open the out of the box ribbon editor. Click ‘+New’ and select ‘Command’ to create a new button. A window pops up that reads: ‘To use PowerFX to build expression based commands we need to create a new component library’, click ‘Continue’ to have the system create the component library. You should see the ‘NewCommand’ button sitting on the ribbon, if not, click the ‘+New’ button and select ‘Command’ again. Click on the newly created button and enter ‘Put on Hold’ as the label. Under the ‘Icon’ section, set it to ‘Use icon’ and select an icon. I used the ‘MarkAsLost’ icon for my button. Under the ‘Action’ section, select ‘Run Formula’ (so that we can run a PowerFX formula when the button is clicked). From here I enter the following PowerFX formula in the formula bar:

Patch(Orders,Self.Selected.Item, {HoldButton:"Active Hold" & Rand()});Notify("Hold in Progress", NotificationType.Success,4000)

Here is an explanation of the PowerFX function: The red colored part of the PowerFX formula updates (patches) the order table (highlighted in green). The self.selected.item represents the order that has been selected from the subgrid. The column that is updated on the selected order is called ‘HoldButton’, and the value that the column is updated with is the words ‘Active Hold’ followed by some random numbers. (The Rand function returns a pseudo-random number that’s greater than or equal to 0 and less than 1.) I am adding this random data so that each time the button is clicked, unique data is entered in the column. The notify part of the formula (highlighted in purple) represents a notification that will be shown on the top of the model-driven app when the button is pressed. The notification text is ‘Hold in Progress’ and it will be shown for 4 seconds.

NOTE: If you created the new column after creating the button(s), you might notice that the new column doesn’t show up when you’re trying to configure your PowerFX formula. In order for the newly created column to show you you’ll need to open the ‘Component Library’ from within the ribbon editor, click on ‘Data’ on the left side of the screen and refresh the data. You can do this by clicking on the three dots on the right side of the table and clicking ‘Refresh’. The last thing I need to do here is configure the visibility of the button. Under the ‘Visibility’ section, set the dropdown to ‘Show on condition from formula’. Then on the top of the screen, change the field that shows ‘OnSelect’ to ‘Visible’ and enter the following PowerFX formula:

CountRows(Self.Selected.AllItems) = 1

This formula will only make the button visible if the number of orders in the subgrid is one. The second button is called ‘Release Hold’ and it’s created in the exact same way I just created the ‘Put on Hold’ button. There is only a slight difference in the PowerFX formula, which is the actual data that will be written into the ‘HoldButton’ column.

Patch(Orders,Self.Selected.Item, {HoldButton:"Remove Hold" & Rand()});Notify("Release in Progress", NotificationType.Success,4000)

Create the Power Automate Flow

Navigate to make.powerautomate.com to start the creation of the flow. I’ll use the Dataverse trigger of when a row is added, modified or deleted. Remember we’re updating the ‘HoldButton’ column in the order table, so this will be the column that will be used to trigger the flow. For the ‘Change Type’ I select ‘Modified’, for the table name ‘Orders’ and in my example I am using organization as the scope. For select columns I enter the system name of the HoldButton column, which in my case is rsm_holdbutton. I enter ‘(contains(rsm_holdbutton, ‘Active’) or contains(rsm_holdbutton, ‘Remove Hold’))‘ in the ‘filter rows’ field. The next step is a condition. Click on the + button and select ‘Add an action’. Then search for the ‘Control’ action from the list. Below is an image of the flow in the legacy designer so all the steps are visible.

Select ‘Condition’ as the action to use in the flow. Click inside the ‘choose a value’ field, then click on the thunder icon. This will allow you to choose dynamic content from the trigger. Search for the ‘HoldButton’ column to enter in the step. Set the middle field to ‘Contains’ and enter the word ‘Active’ in the last field that reads ‘Choose a value’. You’ll notice there is a “true’ and ‘false’ path the flow will take depending on the outcome of the condition. Click on the + button to add another action. Search and select the Microsoft Dataverse ‘Update a row’ action. The table name is ‘Orders’ and for the ‘Row ID’ we’ll need to click on the thunder icon again to select another dynamic value. Search for ‘Order’ and select the order that shows ‘Unique identifier of the order’ below the name. Scroll down until you see the ‘Status Reason’ field and change the status to ‘On Hold’. Under the ‘False’ step below the condition and repeat the same steps. Instead of setting the ‘Status Reason’ field to ‘On Hold’, set it to ‘In Progress’, as this will be the status when the order is no longer on hold. Now it’s time to save and test your flow and your buttons! I hope you enjoyed this article! Be sure to check in again next week for a new article or subscribe here to never miss another post!

Share this!

Leave a Reply