web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Is it possible to crea...
Power Apps
Answered

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

(0) ShareShare
ReportReport
Posted on by 49

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.

Categories:
  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

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

  • GarethPrisk Profile Picture
    2,828 on at

    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.

  • Verified answer
    v-bofeng-msft Profile Picture
    Microsoft Employee on at

    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-PowerApps-in/idi-p/151923

    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/577130#M179919

    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

     

  • AbdulWahab Profile Picture
    517 on at

    Hi

     

    Thanks, It relaly helped me in solving my problem.
    Regards,

     

    Abdul Wahab
    Power Platform & Customer Engagement Developer/Lead/Solution Architecture/Project Manager
    Direct/WhatsApp:+923323281237
    E-mail: abdulwahabubit@outlook.com
    Skype: abdul.wahabubit
    Linkedin: https://www.linkedin.com/in/abdul-wahab-a5b8b011a/
    Youtube Channel: https://www.youtube.com/channel/UCBjgLqLK_2kU-3bwo4McrTw
    Twitter: https://twitter.com/Abdulwahabubit

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 350 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 318

#3
Mohsin Ali Profile Picture

Mohsin Ali 220

Last 30 days Overall leaderboard