cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MrNappa
Advocate V
Advocate V

Bug regarding collections, Gallery.Selected, and patches?

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

3 REPLIES 3
RandyHayes
Super User III
Super User III

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

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!
MrNappa
Advocate V
Advocate V

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
Super User III
Super User III

@MrNappa 

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

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

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

Top Solution Authors
Top Kudoed Authors
Users online (38,687)