How To Call A Function With Parameters From Multiple Screens In PowerApps

Introduction

Now there is no need to write the same formula multiple times in your canvas powerapps. We know that, in javascript, we create different reusable/callable functions and we call them wherever needed. Similar to that approach, in this article, we will learn one simple trick to reuse the formula. It will help you to call the formula/function from different controls or screen navigations.

Use case

I want to create an audit list that tracks the user activity performed on the canvas app. I have two screens, a few simple buttons, a gallery with the delete button. Whenever the user navigates to these screens or clicks on different buttons, a new list item should get created. To track the activity details, we will also pass a parameter to the function call. 

Implementation

To achieve the function call in powerapps, we will make use of toggle control. We will write our reusable formula in the OnChange event of this toggle. Use some boolean variable (say "varCallFunction") as default property of the toggle control. Now whenever you want to call the function, just toggle the boolean value of variable varCallFunction. When the boolean value changes, the OnChange property will get triggered and it will perform the defined operation. To pass the parameters, just make use of variables.

Step 1: Audit List

Create a simple SharePoint list, which has the default Title column. We will save the activity details in the Title column. Our list will look as below:

Step 2: Add a couple of Screens

Let us add a couple of Screens and a few buttons. I have also added a gallery and delete button to the gallery.

Step 3: Add a Toggle control

On any of the screens, add a toggle control. Write your function in the OnChange event of the toggle. In this case, we need to create the record in the audit list, so we will use a patch function as shown below.

Set the default property of the toggle to some boolean variable (say varCallFunction)

Step 4: Call a function

To call a function or formula that we have defined in the OnChange event of the toggle, just invert/toggle the varCallFunction value using !Toggle1.Value. Also, set the parameter value (parameter is nothing but a string variable).

To create an item in the audit list,

a) when user clicks on button 1, OnSelect property of the button1:

Set(param1, "Button 1 clicked!");
Set(varCallFunction, !Toggle1.Value);

b) when user clicks on button 2, OnSelect property of the button2:

Set(param1, "Button 2 clicked!");
Set(varCallFunction, !Toggle1.Value);

c) when user cliks on delete button from the gallery

Remove(PowerAppsAccordion, ThisItem); //This is just to delete the item from gallery
Set(param1, "Item " & ThisItem.ID & "deleted by " & User().FullName);
Set(varCallFunction, !Toggle1.Value);

d) when user navigates to screen 1, OnVisible property of the Screen 1:

Set(param1, "Navigated to Screen 1");
Set(varCallFunction, !Toggle1.Value);

e) when user navigates to screen 2, OnVisible property of the Screen 2:

Set(param1, "Navigated to Screen 2");
Set(varCallFunction, !Toggle1.Value);

Output

I hope you liked this article. If you have any queries please let me know in the comments section below. Thanks for reading :)


Similar Articles