cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
EricLott
Level 10

Using a method in PowerApps

What are y'all using for large blocks of code that you use regularly?

I have a block of code I use a lot that I store in an invisible button's OnSelect property and every time I want to execute it I use

 

Select(MethodName)

 

Where MethodName is actually the button name. The problem is I can only do this on the screen the button is on.

Is there an actual way to do this?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Using a method in PowerApps

Add a Toggle control to any one screen. Set its Default and Reset property to a variable, lets call it: ReUseCodeVariable

Set the OnChange property of the Toggle control to: YourReusableCode; Set(ReUseCodeVariable,false)

In my example, it is: Set(var, false); Set(ReUseCodeVariable,false)

 

Now wherever you want to reuse the code, for example, in the OnSelect property of a button, simply set it to this: 

Set(ReUseCodeVariable,true)

 

You can then hide the toggle if needed. Also, since we are playing with global variables, it can work on any screen. This is heavily inspired by this post: https://ericniemiec.wordpress.com/2018/01/16/how-to-build-reusable-code-snippets-in-microsoft-powera...

 

The only difference is that this post uses a local variable, whereas I used a global variable to make it more useful. I am attaching a small GIF that should give you an idea that it works. 

 

Let me know if this is in line with what you were expecting. 

 

---
If you like this reply, please give kudos. And if this solves your problem, please accept this reply as the solution.

Thanks!
Hardit Bhatia
https://thepoweraddict.com

View solution in original post

2 REPLIES 2
Super User
Super User

Re: Using a method in PowerApps

Add a Toggle control to any one screen. Set its Default and Reset property to a variable, lets call it: ReUseCodeVariable

Set the OnChange property of the Toggle control to: YourReusableCode; Set(ReUseCodeVariable,false)

In my example, it is: Set(var, false); Set(ReUseCodeVariable,false)

 

Now wherever you want to reuse the code, for example, in the OnSelect property of a button, simply set it to this: 

Set(ReUseCodeVariable,true)

 

You can then hide the toggle if needed. Also, since we are playing with global variables, it can work on any screen. This is heavily inspired by this post: https://ericniemiec.wordpress.com/2018/01/16/how-to-build-reusable-code-snippets-in-microsoft-powera...

 

The only difference is that this post uses a local variable, whereas I used a global variable to make it more useful. I am attaching a small GIF that should give you an idea that it works. 

 

Let me know if this is in line with what you were expecting. 

 

---
If you like this reply, please give kudos. And if this solves your problem, please accept this reply as the solution.

Thanks!
Hardit Bhatia
https://thepoweraddict.com

View solution in original post

EricLott
Level 10

Re: Using a method in PowerApps

That's definitely a better solution. Thanks!

Helpful resources

Announcements
thirdimage

Power Apps Super User Class of 2020

Check it out!

thirdimage

New Badges

Check it out!

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors
Users online (5,047)