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 / Reuse code by calling ...
Power Apps
Unanswered

Reuse code by calling a variable

(0) ShareShare
ReportReport
Posted on by

Hello everyone,

 

I am building an app and have many screens (approx. 50) half of which will use the same code to navigate to the next screen. Problem is the different options for this navigation will change and increase over time so I need to have the code in one place so I can update all screens with one change.... so reuse the code.

 

I have looked at using the toggle button but other posts state that this isn't the best solution and often causes errors.

Therefore, I would like to use a component, is this possible by just calling a variable which triggers the component and executes the code?

 

Pasted in below is the code I want to use:

Set(NextCount, Switch(ProdDirOut,
 "Prod1",
 First(Sort(Filter(Prod1, OpCode01 > CurrCount), OpCode01)).OpCode01,
 "Prod2",
 First(Sort(Filter(Prod2, OpCode01 > CurrCount), OpCode01)).OpCode01,
 "Trial",
 First(Sort(Filter(Trial, OpCode01 > CurrCount), OpCode01)).OpCode01
 )
);

Set(NextStep, Switch(ProdDirOut,
 "Prod1",
 LookUp(Prod1, OpCode01 = Value(NextCount), Step),
 "Prod2",
 LookUp(Prod2, OpCode01 = Value(NextCount), Step),
 "Trial",
 LookUp(Trial, OpCode01 = Value(NextCount), Step)
 ));

I watched a couple of online lessons on components, but they don't cover code reuse via variables.

 

Thanks for your help.

 

Rob

Categories:
I have the same question (0)
  • Sundeep_Malik Profile Picture
    6,497 on at

    Hey @rob4681 

     

    I usually use containers and variables to minimize the number of screens. 

    I would really suggest not to use 50 screens, this will decrease the application speed and it wont be user friendly.

     

    So I would suggest the following technique:

     

    1) Add a horizontal container.

    2) Inside that add a normal container. [You can keep its height and width according to outside container].

    3) On the press of buttons you can make global variables that would turn true or false.

    4) Make OnVisible Property of the outside container according to those variables. 

     

    Adding screenshots below:

     

    Here is my container 4 that contains all the things related to appointments:

    Sundeep_Malik_0-1701182756115.png

    If you see above, OnVisible Property of the container is varAppoint.

     

    On the OnSelect property of the Appointments button I have added the following code:

    Set(varAppoint,true);Set(varBranch,false)

    Sundeep_Malik_3-1701183142585.png

     

     

     

    So, when I press the button, it would make the Appointment container visible.

     

     

     

    Now coming to the Branches Container:

    If you see OnVisible Property of the container, I have added varBranch.

    Sundeep_Malik_1-1701182878795.png

     

    Then on the OnSelect Property of the Branches Button I have added:

    Set(varAppoint,false);Set(varBranch,true)

     

    Sundeep_Malik_4-1701183169975.png

     

     

    Now sometimes this does not work initially so what I do is, on the OnVisible Property of the screen, by default I will make the varAppoint as true and varBranch as false. So whenever the screen is loaded, the Appointments container would be visible. Because initially when you start the app, the variables would have a blank value. So, whenever the screen loads it will show you Appointment container defaultly. Or if you dont want this, you can also make the variable true or false on the OnStart Property of the application. 

     

    Sundeep_Malik_2-1701182997212.png

     

    So by the above approach you can decrease the number of screens, and I would highly suggest this approach. Making 50 screens is really too much. 

     

    I hope this helps 🙂

     

  • rob4681 Profile Picture
    on at

    Thanks for the advice but the app just updates a tracking gallery and all other data is patched out so it shouldn't be too slow. 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 531 Most Valuable Professional

#2
Haque Profile Picture

Haque 261

#3
Kalathiya Profile Picture

Kalathiya 221 Super User 2026 Season 1

Last 30 days Overall leaderboard