web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Code reference / reuse...
Power Apps
Answered

Code reference / reuse - functions that can be called?

(1) ShareShare
ReportReport
Posted on by 732

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?

Categories:
  • Verified answer
    mdevaney Profile Picture
    29,993 Moderator on at

    @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 Profile Picture
    732 on at

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

  • mdevaney Profile Picture
    29,993 Moderator on at

    @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 Profile Picture
    732 on at

    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?

  • mdevaney Profile Picture
    29,993 Moderator on at

    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

  • mdevaney Profile Picture
    29,993 Moderator on at

    @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);

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 395 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 328

#3
WarrenBelz Profile Picture

WarrenBelz 260 Most Valuable Professional

Last 30 days Overall leaderboard