cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mark2457
Level: Powered On

Shared Code

Hi All

 

Just getting started with PowerApps. Is there a way to write shared functions instead of replicating code

 

For example, I have a form with 5 buttons that all update the same field, but need to do a lot of condition checking, so it's quite a bit of code to get the new value for the field they are updating. The only difference between the code in each button's OnSelect is an integer value, so in most languages I'd write a proc/function and pass this integer as a parameter. From what I can see in PowerApps, I have to replicate the code in all 5 buttons.

 

Am I missing something fundamental?

 

TIA

Mark

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
mdevaney
Level 8

Re: Shared Code

@Mark2457 

Nope, you are not missing anything.  PowerApps does not currently allow the creation of custom functions.

 

Please go vote for this Idea if you want to see it in PowerApps.  Voting is the best way to make feature you want be implemented in PowerApps.  The Idea status is currently "Under Review" so Microsoft is listening.  Go make your voice heard!  

https://powerusers.microsoft.com/t5/PowerApps-Ideas/An-option-to-create-new-custom-functions-in-Powe...

 

...

 

While PowerApps does not allow for the creation of custom functions you can try to hack-around it.  Here's my take on how to solve your problem by creating a single button to execute code (just like a function) with other buttons as the trigger and using a global variable in the place of paramters.  Its kind of a hacky solution.  

 

Step #1  Create a new button called my_new_button.  Set the Visible property to

 

False

 

Step #2 Put this code in the OnSelect property for each of the 5 buttons.  Change the variable called buttonPressed to something different for each button.  The Select function will trigger the OnSelect property for my_new_button

 

Set(buttonPressed,"btn1");
Select(my_new_button);

 

Step #3  Input all of the code for each button within the OnSelect property of my_new_button.  You can use conditional statements like SWITCH to use what code excutes for which button.  Any code outside a conditional statement would run for all buttons.

 

some code common to all buttons here;
Switch(
    buttonPressed,
   "btn1", some code for button 1,
   "btn2", some code for button 2,
   "btn"3, some code for button 3
);
some more code common to all buttons here;

 

I suggest you test this idea out elsewhere before implementing it within your current app.  Best to understand how the solution works prior to doing something more complicated.

 

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

mdevaney
Level 8

Re: Shared Code

@Mark2457 

Just be careful not to write any additional code after the SELECT function in your 5 buttons or else you could set off a race condition where 2 buttons are excuting code at the same time and the execution might be out-of-order.  

 

Set(buttonPressed,"btn1");
Select(my_new_button);
! No more code allowed here !

 

Wikipedia on Race Conditions:

https://en.wikipedia.org/wiki/Race_condition

View solution in original post

3 REPLIES 3
Highlighted
mdevaney
Level 8

Re: Shared Code

@Mark2457 

Nope, you are not missing anything.  PowerApps does not currently allow the creation of custom functions.

 

Please go vote for this Idea if you want to see it in PowerApps.  Voting is the best way to make feature you want be implemented in PowerApps.  The Idea status is currently "Under Review" so Microsoft is listening.  Go make your voice heard!  

https://powerusers.microsoft.com/t5/PowerApps-Ideas/An-option-to-create-new-custom-functions-in-Powe...

 

...

 

While PowerApps does not allow for the creation of custom functions you can try to hack-around it.  Here's my take on how to solve your problem by creating a single button to execute code (just like a function) with other buttons as the trigger and using a global variable in the place of paramters.  Its kind of a hacky solution.  

 

Step #1  Create a new button called my_new_button.  Set the Visible property to

 

False

 

Step #2 Put this code in the OnSelect property for each of the 5 buttons.  Change the variable called buttonPressed to something different for each button.  The Select function will trigger the OnSelect property for my_new_button

 

Set(buttonPressed,"btn1");
Select(my_new_button);

 

Step #3  Input all of the code for each button within the OnSelect property of my_new_button.  You can use conditional statements like SWITCH to use what code excutes for which button.  Any code outside a conditional statement would run for all buttons.

 

some code common to all buttons here;
Switch(
    buttonPressed,
   "btn1", some code for button 1,
   "btn2", some code for button 2,
   "btn"3, some code for button 3
);
some more code common to all buttons here;

 

I suggest you test this idea out elsewhere before implementing it within your current app.  Best to understand how the solution works prior to doing something more complicated.

 

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

Mark2457
Level: Powered On

Re: Shared Code

Thanks. Sounds like the workaround may work. Thanks for suggestion

mdevaney
Level 8

Re: Shared Code

@Mark2457 

Just be careful not to write any additional code after the SELECT function in your 5 buttons or else you could set off a race condition where 2 buttons are excuting code at the same time and the execution might be out-of-order.  

 

Set(buttonPressed,"btn1");
Select(my_new_button);
! No more code allowed here !

 

Wikipedia on Race Conditions:

https://en.wikipedia.org/wiki/Race_condition

View solution in original post

Helpful resources

Announcements
thirdimage

Power Automate 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

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors
Users Online
Currently online: 174 members 5,930 guests
Please welcome our newest community members: