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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / ClearCollect (or Colle...
Power Apps
Unanswered

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

(1) ShareShare
ReportReport
Posted on by 192

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

Categories:
I have the same question (0)
  • Verified answer
    Brian Dang Profile Picture
    3,976 on at

    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.

  • Olivier Profile Picture
    192 on at

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

    Thanks again,

     

    Olivier

  • Olivier Profile Picture
    192 on at

    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

  • Community Power Platform Member Profile Picture
    on at

    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

  • Brian Dang Profile Picture
    3,976 on at

     

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

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard