cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
WebPortal
Post Prodigy
Post Prodigy

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

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!

2 ACCEPTED SOLUTIONS

Accepted Solutions
mdevaney
Super User
Super User

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

View solution in original post

RandyHayes
Super User
Super User

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

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

5 REPLIES 5
mdevaney
Super User
Super User

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

View solution in original post

WebPortal
Post Prodigy
Post Prodigy

😪

 

Thanks anyway for letting me know @mdevaney 

RandyHayes
Super User
Super User

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

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

WebPortal
Post Prodigy
Post Prodigy

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
Super User
Super User

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

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

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 (2,910)