Skip to main content

Notifications

Power Apps - Building 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

  • rob4681 Profile Picture
    rob4681 on at
    Re: Reuse code by calling a variable

    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. 

  • Sundeep_Malik Profile Picture
    Sundeep_Malik 6,480 on at
    Re: Reuse code by calling a variable

    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 🙂

     

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,636

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,942

Leaderboard