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

Invoking App Run on Start on demand

I want to be able to call Run on Start for the app by the OnVisible action of a Screen.  Basically, I have a collection that is utilized on multiple screens and needs to be refreshed based on actions on each of those screens.  Ideally, I would like to have the code for that collection reside in a single place and be able to run that from multiple screens so I don't have to copy and past that collection code in each screen and at each point on each screen where it needs to be refreshed.  Is this possible or does anyone have a suggestion for how to accomplish this?

 

Thanks.  

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@rpitts 

Another method you can do to put your Formulas in one place is what I call a dynamic variable.

 

You would need to get rid of the collection (as you most likely don't need it anyway) and simply utilize this global "variable".

 

Here's how it works...

There are really only two controls in the PowerApps arsenal of controls that will give you IT's items.  Those are the Gallery (the AllItems property - which is a bit of extra overhead you don't need) and a DataCard.

When I mention DataCard everyone thinks - Forms.  But this is not the case, there is one other control that contains a datacard, and that is a Canvas control - this is perfect for this (and in fact, from our app design perspective we probably use Collections less than 1% of the time and these DataCards over 99% of the time - They work great and completely change how you design your apps, not to mention speed them up!).  

 

Now, you cannot add a Canvas or DataCard directly from the insert menu.  So what to do?  You need to add a new screen to your App, and it needs to be a Scrollable screen.  The scrollable screen will add a canvas and a datacard in it.  You can cut and paste the entire Canvas control from that screen to another of your choice - OR leave it where it is - it is Global.  You are not going to make it visible or do anything else with it in terms of controls, so where it is and what size it is will not be important.

 

NOW...the fun!!

There is an Update property in the Datacard.  This is just like the one on a regular form datacard, but without all the form connections.  You can put anything you want in there as long as it is a record.

We will use this property.

 

Let's set the stage - you have a Gallery on your screen and another on another screen as well as a Chart and a Combobox - all using the exact same data (i.e. formula). AND, let's say there is a Dropdown that depending on what is selected, will determine the data for all of those items properties.

This is annoying for many reasons:  1) if it is collection based, then you have to constantly, and in every place, need to recollect the data.  2) You have now duplicated formulas - if you need to change something, you have to do it in every place 3) it becomes a challenge to figure out what is where and where you might have forgotten to change something.

 

Now, back to our Update property.  In there we put a formula such as :

{
    MyItems:
        Filter(someData, someColumn=Dropdown1.Selected.Value)
}

Your Update property is now giving us a record that has a table in it that is based on the Filter which is based on the Dropdown.

For our Galleries, we will simply replace the Items properties with : theDataCardName.Update.MyItems

Same for the combobox and the chart, etc.

 

Taking it a step further, we can add to this record even more things.  So, let's say that our Galleries need slightly different data and our chart as well.  First gallery wants just the Items, the second will have a calculated Sum of all the items, the Chart will want its data as Labels and Series, and our Combobox will have all the Items, PLUS we want to have an "All" as the first item in the list. We can do ALL of that in one place AND it will be dynamic based on the Dropdown (in this case).

 

So...Something like this:

With(_items: Filter(someData, someColumn=Dropdown1.Selected.Value)},

  {
    MyItems: _items,
    Gallery2Items : AddColumns(_items, "someSum", Sum(_items, SomeValueColumn),
    ChartItems: AddColumns(GroupBy(_items, "someLabelColumn", "_recs"), "seriesValue", Sum(_recs, someValue))
    ComboboxItems: ForAll(Sequence(CountRows(_items)+1, 0), {Item: If(Value=0, "All", Last(FirstN(_items, Value)).someColumnforCombobox)})

  }

)

 

In the above we just got all of the items tables for 4 controls ALL in one place and all based on the Dropdown in our app.

No collections needed and we don't have to do anything to instantiate this formula to evaluate and have the correct values (no Onxxx actions, no behavioral actions, etc.)  It all just changes dynamically based on our Dropdown.

 

Again, this method works great and the sky is the limit on what you put in to these.  Best part is that your formulas are all in one place and you completely cut out SO many extra formulas that you would normally need to make the above happen in other ways.

 

I do have a video and post coming out on this topic, but I am backlogged on these at the moment...so, the above is kind of a sneak-peek at what that will be like.

 

I hope this is helpful for you.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

5 REPLIES 5
HenryARPhillips
Continued Contributor
Continued Contributor

Hi,

I am pretty confident when I say that the on start of an app code, is limited to just executing on the start of an app or manually running it when editing an app.

In terms of something you could do to facilitate your need. Now this doesn't address the one place but I do find it helps when you need to contain a bit of logic that could be easily executed in multiple actions on one screen.

 

That is to get the code in the On Select property of a button. Hide the button and just select the button whenever you want to execute that bit of code.

 

Thanks

 

Henry

RandyHayes
Super User
Super User

@rpitts 

Another method you can do to put your Formulas in one place is what I call a dynamic variable.

 

You would need to get rid of the collection (as you most likely don't need it anyway) and simply utilize this global "variable".

 

Here's how it works...

There are really only two controls in the PowerApps arsenal of controls that will give you IT's items.  Those are the Gallery (the AllItems property - which is a bit of extra overhead you don't need) and a DataCard.

When I mention DataCard everyone thinks - Forms.  But this is not the case, there is one other control that contains a datacard, and that is a Canvas control - this is perfect for this (and in fact, from our app design perspective we probably use Collections less than 1% of the time and these DataCards over 99% of the time - They work great and completely change how you design your apps, not to mention speed them up!).  

 

Now, you cannot add a Canvas or DataCard directly from the insert menu.  So what to do?  You need to add a new screen to your App, and it needs to be a Scrollable screen.  The scrollable screen will add a canvas and a datacard in it.  You can cut and paste the entire Canvas control from that screen to another of your choice - OR leave it where it is - it is Global.  You are not going to make it visible or do anything else with it in terms of controls, so where it is and what size it is will not be important.

 

NOW...the fun!!

There is an Update property in the Datacard.  This is just like the one on a regular form datacard, but without all the form connections.  You can put anything you want in there as long as it is a record.

We will use this property.

 

Let's set the stage - you have a Gallery on your screen and another on another screen as well as a Chart and a Combobox - all using the exact same data (i.e. formula). AND, let's say there is a Dropdown that depending on what is selected, will determine the data for all of those items properties.

This is annoying for many reasons:  1) if it is collection based, then you have to constantly, and in every place, need to recollect the data.  2) You have now duplicated formulas - if you need to change something, you have to do it in every place 3) it becomes a challenge to figure out what is where and where you might have forgotten to change something.

 

Now, back to our Update property.  In there we put a formula such as :

{
    MyItems:
        Filter(someData, someColumn=Dropdown1.Selected.Value)
}

Your Update property is now giving us a record that has a table in it that is based on the Filter which is based on the Dropdown.

For our Galleries, we will simply replace the Items properties with : theDataCardName.Update.MyItems

Same for the combobox and the chart, etc.

 

Taking it a step further, we can add to this record even more things.  So, let's say that our Galleries need slightly different data and our chart as well.  First gallery wants just the Items, the second will have a calculated Sum of all the items, the Chart will want its data as Labels and Series, and our Combobox will have all the Items, PLUS we want to have an "All" as the first item in the list. We can do ALL of that in one place AND it will be dynamic based on the Dropdown (in this case).

 

So...Something like this:

With(_items: Filter(someData, someColumn=Dropdown1.Selected.Value)},

  {
    MyItems: _items,
    Gallery2Items : AddColumns(_items, "someSum", Sum(_items, SomeValueColumn),
    ChartItems: AddColumns(GroupBy(_items, "someLabelColumn", "_recs"), "seriesValue", Sum(_recs, someValue))
    ComboboxItems: ForAll(Sequence(CountRows(_items)+1, 0), {Item: If(Value=0, "All", Last(FirstN(_items, Value)).someColumnforCombobox)})

  }

)

 

In the above we just got all of the items tables for 4 controls ALL in one place and all based on the Dropdown in our app.

No collections needed and we don't have to do anything to instantiate this formula to evaluate and have the correct values (no Onxxx actions, no behavioral actions, etc.)  It all just changes dynamically based on our Dropdown.

 

Again, this method works great and the sky is the limit on what you put in to these.  Best part is that your formulas are all in one place and you completely cut out SO many extra formulas that you would normally need to make the above happen in other ways.

 

I do have a video and post coming out on this topic, but I am backlogged on these at the moment...so, the above is kind of a sneak-peek at what that will be like.

 

I hope this is helpful for you.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

Randy, thank you!  You have seriously blown my mind.  Just beginning to play with this but I think this is going to simplify so any things.  And now I have my work cut out for me redesigning my apps.  🙂  Thanks again.

 

- Randy Pitts

RandyHayes
Super User
Super User

@rpitts 

It makes all the difference in the world with PowerApps design!!  

I will have a video out on this soon.  If you're interested, subscribe to my channel with alerts (video link below in my signature line) and you'll get a notice when it is published.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
markfleet
Advocate II
Advocate II

Thank you for the Datacard tip @RandyHayes. I had up until now been using multiple galleries for this but having multiple items in a single Datacard's record is much nicer!

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Power Apps Community Call Jan. 2022 768x460.png

Power Apps Community Call

Please join us on Wednesday, January 19th, at 8a PDT. Come and learn from our amazing speakers!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Solution Authors
Top Kudoed Authors
Users online (2,059)