cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BrianHFASPS
Continued Contributor
Continued Contributor

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
Community Champion
Community Champion

@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
Community Champion
Community Champion

@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."

BrianHFASPS
Continued Contributor
Continued Contributor

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
Continued Contributor
Continued Contributor

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
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (2,247)