cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Olivier
Level 8

ClearCollect (or Collect) on "OnVisible" event for screen object not working

What is the best practice to collect data from a datasource into a collection when the application start? I have a ClearCollect(MyCollection, Filter(Datasource, FilterCondition)) in the OnVisible event of the screenObject, and the Gallery on that screen that connects to the collection doesn't show anything.  Looking at the Collection itself (content Tab, Collections), it shows the Table headers, but is empty. When I insert a button with the same formula in the "OnSelect" event, it works. 

When is the OnVisible event firing? Isn't it when the application starts? (play button in the PowerApps studio).

Is there a better way to initialize a collection with content at "App start time"?

Thank you,

 

Olivier

1 ACCEPTED SOLUTION

Accepted Solutions
mr-dang
Level 10

Re: ClearCollect (or Collect) on "OnVisible" event for screen object not working

OnVisible will trigger when you come upon the Screen. This takes place when you open the app and when you Navigate to the screen. For actions I only want done once, I change OnVisible to:

If(!loadapp,
ClearCollect(collection,Filter(datasource,condition));
UpdateContext({loadapp: true})
)

This means that if the variable loadapp is false, which is default, it will make the collection, then set loadapp to true. This is irreversible since nowhere else updates the variable to false again.

 

If you are editing in PowerApps Studio and you want to see your OnVisible property work, then you need to either reopen the app with your OnVisible properties saved or you need to Navigate away to another Screen, then Navigate back.

 

EDIT: clarification: pressing the Play button does not trigger the OnVisible property.

Microsoft Employee
@8bitclassroom

View solution in original post

5 REPLIES 5
mr-dang
Level 10

Re: ClearCollect (or Collect) on "OnVisible" event for screen object not working

OnVisible will trigger when you come upon the Screen. This takes place when you open the app and when you Navigate to the screen. For actions I only want done once, I change OnVisible to:

If(!loadapp,
ClearCollect(collection,Filter(datasource,condition));
UpdateContext({loadapp: true})
)

This means that if the variable loadapp is false, which is default, it will make the collection, then set loadapp to true. This is irreversible since nowhere else updates the variable to false again.

 

If you are editing in PowerApps Studio and you want to see your OnVisible property work, then you need to either reopen the app with your OnVisible properties saved or you need to Navigate away to another Screen, then Navigate back.

 

EDIT: clarification: pressing the Play button does not trigger the OnVisible property.

Microsoft Employee
@8bitclassroom

View solution in original post

Olivier
Level 8

Re: ClearCollect (or Collect) on "OnVisible" event for screen object not working

Thank you for the detailed explanation, I will let you know how it works as soon as I try it. 

Thanks again,

 

Olivier

Olivier
Level 8

Re: ClearCollect (or Collect) on "OnVisible" event for screen object not working

Also, you say "since it is false by default" (talking about the context variable loadapp), I thought that a context variable was "inexistant" (null?) before a first assignment, is a context variable false even before it was ever used?

Thanks,

 

Olivier

Anonymous
Not applicable

Re: ClearCollect (or Collect) on "OnVisible" event for screen object not working

Some observations;

 

 

When you test a variable value, the container for the variable must exist, even if the variable does not have a value yet.

In other words, if I type in the following for the OnVisible action;

 

 

If(!contextvariable, true, false)

It will give me an 'invalid name' error for contextvariable - because nowhere else on the page have I actually created the context container with an UpdateContext formula.  It hasn't been defined yet, so I have nothing to test.

 

 

Note, defining a container for a variable and setting the variable to something are two separate things.

If I add a button and set the OnSelect property to;

 

UpdateContext({contextvariable: true})

 

 

Two things happen as I type it;

  1. First, my initial OnVisible formula becomes valid, because contextvariable has now been defined.  
  2. Second, the data type of the value I'm setting is now applied to the container.  In other words, by specifying a boolean value for the UpdateContext value, the container is automatically set to that type.  NOTE:  I have not set the variable yet or run the screen - I have only typed in formula in design view.

NOTE: This is all before the button is actually pushed - simulating the OnVisible action.

 

Now, this is where it gets interesting.

Before the variable has been set, the following tests on the container yield the following results;

 

!contextvariable = true 
IsBlank(contextvariable) = true 
IsEmpty(contextvariable) = false 

 

While it hasn't been set yet, if you test it to see if it isn't true you will be told that indeed, it is not true.  This does not however mean it is false, simply that it is not set to boolean true.  If you actually look at the variable value, there's nothing in it.  It's blank.

 

This might seem like a long way of saying "It's not false by default, it's blank", but here's something else;

 

If you change the Onselect formula to this;

UpdateContext({contextvariable: 10})

We have effectively changed the data TYPE of the container - still without actually pushing the button.

Now the OnVisible tests look like this;

 

!contextvariable = false
IsBlank(contextvariable) = true 
IsEmpty(contextvariable) = false 

 

It's still blank, but now the !contextvariable test comes back false, instead of true.

This could seriously mess with your head if, for some reason, you're using numbers in this variable Man LOL

 

 

Once you set it by pushing the button, when next you run OnVisible the results will be;

!contextvariable = false
IsBlank(contextvariable) = false
IsEmpty(contextvariable) = false 

 

So, long story short, it seems to be blank by default.  Testing if it isn't boolean true comes back true simply because it isn't boolean true - but that doesn't mean it's boolean false.  It's blank.

It never seemd to be empty at any point.

It's only true for logical negation (!contextvariable which is the same as testing IS NOT TRUE) when the type is not a number.  When the type is a number, it returns false.  Maybe someone can explain why this is a little better Man Frustrated

mr-dang
Level 10

Re: ClearCollect (or Collect) on "OnVisible" event for screen object not working

 

@Anonymous,

I think the PA team called it "null" when the variable hasn't been determined. It just happens to work like 'false' if it hasn't been defined. The behavior of variables can seem wonky. If you're revising any part of how the variable is defined in UpdateContext, it gets reset to null.

 

Microsoft Employee
@8bitclassroom

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors (Last 30 Days)
Users online (4,730)