cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BrianHFASPS
Responsive Resident
Responsive Resident

Code reference / reuse - functions that can be called?

I have a piece of code about 50 lines long that runs a Refresh() on an SQL View and then builds a table via some AddColumns(). I use this in 3 buttons on one screen and another button on a different screen. Is there a way to package that code and call it like a funciton in other languages? I have it all built but I am worried that when I tweak it I might forget to update it in the multiple places. I don't want to get rid of the buttons for many ease of use and UI reasons. Is there any way to do this in PowerApps?

1 ACCEPTED SOLUTION

Accepted Solutions
mdevaney
Super User
Super User

@BrianHFASPS 

There is a technique for buttons on the same screen.

 

Say you have 3 buttons Button1, Button2, Button3 all with the same code in the OnSelect property 

 

line_1;
line_2;
line_3;
...
line_50;

 

To avoid repetition you can create another button named function_RefreshSQL and put the 50 lines of code in the OnSelect property.

 

line_1;
line_2;
line_3;
...
line_50;

 

Its a good idea to hide the function_RefreshSQL button since we dont want the user to click it.  Change the Visible property to the following code.

 

false

 

Now go back to Button1, Button2, Button3 and change the code in the OnSelect property the code below.  When any of those buttons are clicked on it will run the code in function_refreshSQL instead.

 

Select(function_refreshSQL)

 

This only works for buttons on the same screen.  One creative way to get around that limitation would be to build your entire app on a single screen.  Whether you want to do that will depend on your own preferences

 

Link to documentation

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-select

 

---
Please click "Accept as Solution" if my response helped to solve your issue so that others may find it more quickly. If your thought the post was helpful please give it a "Thumbs Up."

View solution in original post

6 REPLIES 6
mdevaney
Super User
Super User

@BrianHFASPS 

There is a technique for buttons on the same screen.

 

Say you have 3 buttons Button1, Button2, Button3 all with the same code in the OnSelect property 

 

line_1;
line_2;
line_3;
...
line_50;

 

To avoid repetition you can create another button named function_RefreshSQL and put the 50 lines of code in the OnSelect property.

 

line_1;
line_2;
line_3;
...
line_50;

 

Its a good idea to hide the function_RefreshSQL button since we dont want the user to click it.  Change the Visible property to the following code.

 

false

 

Now go back to Button1, Button2, Button3 and change the code in the OnSelect property the code below.  When any of those buttons are clicked on it will run the code in function_refreshSQL instead.

 

Select(function_refreshSQL)

 

This only works for buttons on the same screen.  One creative way to get around that limitation would be to build your entire app on a single screen.  Whether you want to do that will depend on your own preferences

 

Link to documentation

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-select

 

---
Please click "Accept as Solution" if my response helped to solve your issue so that others may find it more quickly. If your thought the post was helpful please give it a "Thumbs Up."

View solution in original post

BrianHFASPS
Responsive Resident
Responsive Resident

Just checking the RefreshSQL button can be non-visible to hide from user right?

@BrianHFASPS 

 

Additionally, you might want to vote for the Idea using the link below

https://powerusers.microsoft.com/t5/PowerApps-Ideas/Create-custom-functions-macros/idi-p/6187

BrianHFASPS
Responsive Resident
Responsive Resident

On other question, can I trigger the button during a Navigate call? I only need the code as I enter the one screen and as the user uses that screen so that could work maybe?

You are 100% correct sir!

 

By the way, I really enjoy your questions.  There was another one I helped with earlier called "Re: CountRows = 0 but IsBlank = false".   I am going to share the answers to both questions with my team to highlight some of the quirks of PowerApps.  Very helpful!

 

See you around the forums!  Cat Happy

@BrianHFASPS 

My first suggestion would actually be to put this code within the OnVisible property of your Screen instead.  If you use the code after your Navigate function on another screen PowerApps will throw an error because  fn_refreshSQL is not on the same screen.

 

Select(fn_refreshSQL);

 

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (3,772)