Returning data to Power Apps
When we think about functions and processes from a pro-code perspective, more often than not we pass data into a function to get something back. We can apply exactly the same thinking to our flows. As well as having a trigger to bring data in, we have an action that will allow us to return data from the flow to the app.
When we are looking for Power Apps actions, we need to follow the same process as we did for Outlook: search for the connector, and then select your action. So this time, when we search for our connector, we will search for PowerApps
.
There are certain areas where the branding for Power Apps hasn’t been updated, and Power Automate is unfortunately one of them. So make sure you search for it without the space!
When we have located our connector, we can then select the Respond to a PowerApp or flow action:
Figure 14.17: Searching for a connecter to find actions
When we use this action, we can configure it in exactly the same way as we did with our trigger. Rather than defining inputs, instead we define outputs, by selecting + Add an output and selecting the type of data that we want to return.
When running the flow in test mode, the Respond to a PowerApp or flow action will always show as skipped. It will only fire when actually called from an app.
In our example, we will just return a simple response to the app to say that the email has been sent, returning a Boolean (yes/no) value. Rather than being asked to provide the description this time, as this is only available to the flow, we are asked to provide the value that we want to send back.
Just like with our email, we could use dynamic content to take the outputs of a previous action to send back to the app, or we could return the outputs of an expression. To keep it simple at the moment, let’s just return the value true
.
To do this, we will use the dynamic content toolbox, but this time we will select the Expression tab and use the expression true
to return the Boolean value to our app:
Figure 14.18: Responding to the Power App by returning a Boolean value
Now that we have looked at the ways in which we can build our flow, we need to look at how we can use this within our apps.
Calling a flow within Power Apps
Being able to use flows to handle all of our heavy processing is a great feature of the Power Platform. It means that we can keep our apps as light as possible, or reduce some of the more complex formulas to something that we can build visually.
In order to use our flow, we need to have an event that we can use to trigger the flow, such as the OnSelect event for a button, or the OnSuccess event for a form. To associate a flow and then use it, follow these steps:
- The first thing we need to do is decide where we want the flow to be executed. Do we want it to be executed from the screen or from a control? In this example, we’re going to execute the flow from a button, so we will place a button on the screen and ensure that it has been selected, as shown in the following screenshot:
Figure 14.19: Placement of a button that will execute the flow
- With the desired control selected, we need to navigate to the Power Automate button, on the left side of the screen. This will present a list of flows that are either already associated with the control or available for association. If you don’t see your flow listed, then you will need to make sure that your flow has been saved, and ensure that both the app and the flow are located in the same environment.
Look for the flow that you want to use and take note of the flow name on the second row of text, as this is what you will need to know when calling it. In this example, it is PowerAppsTriggeredFlow:
Figure 14.20: Flow name
- To associate the flow with the control, we simply need to click on the desired control and property that we are going to use to execute the flow, such as the OnSelect event. We can then start typing the flow name identified in the previous step into our formula:
Figure 14.21: Executing a flow using an event
- Selecting the
.Run
formula then allows us to provide any arguments to our flow to make it run. In this example, we want to provide the email address of the user who is logged in by using theUser()
formula. For this we would use the following formula:PowerAppsTriggerFlow.Run(User().Email)
Now, when the Run flow button is pressed, our app will pass the current user email value to execute the logic contained within the flow. Creating our formula in this way will allow us to push data to the flow, but not capture what is returned. In order to capture the returned data, we need to use a variable.
If we think back to earlier in the book, we created variables to temporarily store the data, and we can use the exact same formulas here. If we use a global variable, we would use the formula:
Set(gblFlowHasCompleted,PowerAppsTriggeredFlow.Run(User().Email))
Figure 14.22: Flow result from clicking the Run flow button in Power Apps
Once we have the output of the flow caught within a variable, we can then use that elsewhere in our app, e.g., we could then show a success message when the value is returned from the flow.
Now that we have covered the key components of creating flows, and how they interact with Power Apps, let’s now add this to a lab-based app.