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 / Run the code of the On...
Power Apps
Answered

Run the code of the OnSelect property of a button when a screen becomes visible

(0) ShareShare
ReportReport
Posted on by 2,297

I want to run the code of the OnSelect property of a button when a screen becomes visible.

Instead of replicating the same code into the OnVisible property of that screen, is there a cleaner way?

Thanks!

Categories:
I have the same question (0)
  • Verified answer
    mdevaney Profile Picture
    29,991 Moderator on at

    @WebPortal 

    No, there is not a cleaner way.  Power Apps does not have any way to create 'functions' yet.

    ---
    Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

  • WebPortal Profile Picture
    2,297 on at

    😪

     

    Thanks anyway for letting me know @mdevaney 

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @WebPortal 

    Yes, you can do this easily in a couple different ways.  One of the easiest is to simply Select the button in the OnVisible action of the screen - Select(yourButton)

     

    Another way to simulate functions is with the use of a toggle.  We do this one very often.

    The concept is to utilize a variable to determine the function to perform.  

    Example, a variable called lclFunction.  We would set the variable as such UpdateContext({lclFunction:"ResetScreen"})

     

    The toggle has a Default property of !IsBlank(lclFunction) and then in the OnCheck action of the Toggle is a formula such as this:

    Switch(lclFunction,
    
     "ResetScreen",
     Reset(control1);
     Reset(control2), //etc
    
     "RefreshData",
     Refresh(dataSource),
    
     "SubmitData",
     SubmitForm(form1)
    );
    UpdateContext({lclFunction:Blank()}) // note, this is important in your OnCheck action.

    So, setting the lclFunction variable anywhere in the screen will fire the toggle and the named action will happen.

     

    It's kind of a "semi-function" concept and it really serves to put Formulas in one place rather than many hard to find places. 

    So, in your case, you can put your Formula in the toggle OnCheck and in the OnVisible of the screen you can put a formula such as : UpdateContext({lclFunction:"myButtonFormula"})  Then on the OnSelect of the Button the same thing: UpdateContext({lclFunction:"myButtonFormula"})  

     

    The concept works well and is clean.  The nice part is you can consolidate a lot of formulas in one place for easier maintenance.

     

    I hope this is helpful for you.

  • WebPortal Profile Picture
    2,297 on at

    Whoa!

     

    This is really cool! You manage to simulate functions, which are highly necessary in anything apps.

     

    Thanks a lot for all this info, I'll save it!

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @WebPortal 

    Well...Sort of simulate a function.  Functions usually have some input and return. Technically you don't get that with this, but you can at least consolidate formulas and simulate a lot of things.

    I am glad you liked it.  I actually have a video on how to do this.  It's just not completed and published yet - never enough time in a day!  I'll get it there eventually.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 522

#2
WarrenBelz Profile Picture

WarrenBelz 437 Most Valuable Professional

#3
Vish WR Profile Picture

Vish WR 405

Last 30 days Overall leaderboard