Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Unanswered

Bug regarding collections, Gallery.Selected, and patches?

(0) ShareShare
ReportReport
Posted on by 241

In my app, at a moment where I know the user is connected to the internet, the user can load some data. This is stored locally in a collection and subsequently displayed in a gallery. The user can then click gallery items to select them and perform operations etc. However, since recently, I'm running into some issues...

 

I will illustrate how I set this up technically as it's really easy to set this up to demonstrate.  (I'm using 'Accounts' datasource in this example -- but in reality I'm working with another custom entity) 

 

In an action, for example in the OnSelect code of a button, load some data:

 

Clear(local_accounts);
ForAll( FirstN( Accounts, 3 ),
 Collect( local_accounts, {
 guid: Account,
 locName: 'Account Name',
 locAddr: 'Address 1: City' & " (" & 'Address 1: ZIP/Postal Code' & ")"
 });
);

 

Load this data into a gallery, and set its OnSelect action to Set( selected_account, Gallery2.Selected );
This is where the trouble recently seems to have started, where it wasn't an issue before. As I said, I allow some data to be changed locally (so I can patch it back in a later moment). For example, I would do something like this:

 

// determine a new value
UpdateContext( { new_val : "["&Now()&"]" } );
// update the record in the local collection
Patch( local_accounts, selected_account, { locAddr: new_val } );
// also update the variable to reflect the new values
Set( selected_account, Patch( selected_account, { locAddr: new_val }) );

 

This would properly perform all updates (locally). But now, instead of patching the selected record in the local_accounts collection, the Patch command instead adds a new entry in the local_accounts collection that is empty except for the 'locAddr' column.

 

Am I off the rails here? Or is this a bug? As I said, up until recently, this method worked perfectly fine. I did notice that the "Selected" property of the gallery now seems to include some columns that are actually UI elements, that I never noticed before: 

clip.png

But I don't know if that's got something to do with it...

Categories:
  • RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on at
    Re: Bug regarding collections, Gallery.Selected, and patches?

    @MrNappa 

    Yes, referencing the controls directly is always preferred over setting snapshot variables that you then have to maintain in your app.  

  • MrNappa Profile Picture
    241 on at
    Re: Bug regarding collections, Gallery.Selected, and patches?

    Thanks, some good insights there. It seems as though your suggested solution (Set(LookUp(UpdateIf(..)))) seems to work.  

     

    I was looking for a solution myself in the meantime, it looks like I could also prevent the problem from occurring by removing the 'direct' gallery selection property from the assignment like this:

    Set( selected_account, LookUp( local_accounts, guid=Gallery2.Selected.guid ) );

    I think this works because now there is no "hidden discrepancy" between what I assumed was the same data type; (selected_account was set to Gallery.Selected which consisted of not only the local_accounts columns but also the extra UI columns).

    The advantage there is that I don't have to manually go through the my app looking for all places where I patch the

    local record... (OTOH for sake of efficiency I still might want to do that anyway). 

     

    Not entirely sure why it never gave me problems before.

  • RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on at
    Re: Bug regarding collections, Gallery.Selected, and patches?

    @MrNappa 

    When you reference a row in a gallery it includes not only the underlying data elements, but also any controls that exist in the Gallery - this is a feature that has always made a Gallery a ever useful and powerful control.

     

    When you issue a Patch statement and include a full record as the record to patch, it will utilize the primary key of the record and any other columns you provide in the record to identify which record in the data table to patch.

    If you are setting a snapshot variable to the selected record in the Gallery, and your collection is based on a datasource (and not the gallery) then the records will be different.  Once patch cannot find an exact match to the record, it will create a new one (this goes very well for when you work directly with the datasource and not some collection).

     

    Consider the following changes:

    ClearCollect( local_accounts, 
     ForAll( FirstN( Accounts, 3 ),
     {
     guid: Account,
     locName: 'Account Name',
     locAddr: 'Address 1: City' & " (" & 'Address 1: ZIP/Postal Code' & ")"
     }
     )
    );

    The above change just so that you are not wasting the output of the ForAll function as it returns a Table.

     

    The OnSelect of the Gallery:

    Set(selected_account, ThisItem)

     

    // determine a new value
    // update the record in the local collection
    // also update the variable to reflect the new values
    Set(selected_account, 
     LookUp(
     UpdateIf( local_accounts, guid=selected_account.guid, { locAddr: "["&Now()&"]" } ),
     guid=selected_account.guid
     )
    )
    

    UpdateIf will identify the record to update directly by the primary key.  It will update to the value you want.  

    UpdateIf returns a table of the local_accounts table with the updated records.  The LookUp function is applied to get the unique record in order to reset the snapshot variable.

     

    I hope this is helpful for you.

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,552 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,928 Most Valuable Professional

Leaderboard