cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper I
Helper I

Is it possible to create functions or re-use code in different places?

I have some 20-30 line pieces of code running when various buttons are pressed. Is there a way to write this code once, and then call it in different places? Having to make sure I make changes everywhere is making my app pretty vulnerable on even small changes.

3 REPLIES 3
Highlighted
Community Champion
Community Champion

Re: Is it possible to create functions or re-use code in different places?

@brucemZA 

You can use Toggles to achieve this but in the past it has been problematic. I've used it in a test app but haven't open that one in awhile to see it it's still working as expected.

Highlighted
Solution Sage
Solution Sage

Re: Is it possible to create functions or re-use code in different places?

Unfortunately, it's not possible to invoke a function like you would using something like JavaScript.

 

However, the pattern of organizing a function into a single place so it can be used over and over is possible. There are some considerations, though.

 

A common example is a 'Loading Screen's with a Button per type of load. Then I can Navigate to the 'Loading Screen' with a context variable, like "LoadAccounts" which the onVisible property of the screen can use to select the button.

 

This way, if my function to load accounts is complicated, I can still get to it for multiple places, and even go so far as to pass a screen variable so the button can automatically navigate the user back.

 

This has another advantage of giving you a blocking pattern to allow the app to process data and letting the user know you're doing X, Y, Z. Bonus if you have a loading.io spinner or something.

 

NOTE: the function Select() is used to click the button, but it's the same as a user clicking. If you had three buttons (each with complicated functions), you should consider having the last like in each select the next button (if they have to be sequential), otherwise three select functions in a row is the same as the user (very) quickly and starting the three functions in parallel.

Highlighted
Microsoft
Microsoft

Re: Is it possible to create functions or re-use code in different places?

Hi @brucemZA

Do you want to encapsulate a long piece of code as a custom function and then you can use this function by calling the function name?

The most direct answer is: This is an idea under review.

If you want to implement this feature, please vote for this idea:

https://powerusers.microsoft.com/t5/Power-Apps-Ideas/An-option-to-create-new-custom-functions-in-Pow...

In addition, there are three alternatives:

1\Using Flow.

You can call Flow in PowerApps, and Flow can return specific results to PowerApps. In other words, you can delegate the data processing process to flow, and you only need to fill in the name of the flow and the necessary parameters for each call.

The following connection is a case I made (1\using a button in the app to trigger the flow. 2\the flow stores the picture to onedrive and then gets the sharing connection. 3\The flow returns the sharing connection to powerapps)

https://powerusers.microsoft.com/t5/Building-Power-Apps/capture-photo-and-save-image-locally/m-p/577...

I think this link will help you a lot:

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/using-logic-flows

2\Createing a custom connection.

It's not a simple process today. The main process to add a custom function is to create a custom API, where the function would be executed in the server, and in your app you'd add a connection to that API.

3\Using custom Components.I'v made a test for your reference:

Components are reusable building blocks for canvas apps so that app makers can create custom controls to use inside an app, or across apps using a component library. Components can use advanced features such as custom properties and enable complex capabilities. 

In other words, you can encapsulate the data processing process into a custom Component, and then implement code reuse by using this Component.

I assume that the formula I want to customize is number1*number2.

Step1: add a compentent

In this compentent

Step2: add two cunstom proeprty(Input1/Input2)

  • property type:Input
  • Data type:number

Step3: add a cunstom proeprty(Output1)

  • property type:output
  • Data type:number

Step4: add a label control(Label1_2)

Text

 

Parent.Input1*Parent.Input2

 

Step5: set this component's Output1 property to:

 

Value(Label1_2.Text)

 

1.JPG

Back to my screen and add a custom component(Component1_4)

step6\Add two textinput controls(TextInput3/TextInput3_1)

step7\set the Component1_4

Input1

 

Value(TextInput3.Text)

 

Input2

 

Value(TextInput3_1.Text)

 

step7\Add a label and set it's Text preoeprty to:

 

Component1_4.Output1

 

26.gif

Best Regards,

Bof

 

Helpful resources

Announcements
Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Super Users Coming in August

We are excited for the next Super User season.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (7,304)