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?
Solved! Go to Solution.
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."
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."
Just checking the RefreshSQL button can be non-visible to hide from user right?
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
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!
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);
User | Count |
---|---|
124 | |
87 | |
86 | |
75 | |
69 |
User | Count |
---|---|
214 | |
181 | |
139 | |
96 | |
83 |