cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mark2457
Helper II
Helper II

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

@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 

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

@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

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

@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
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (1,600)