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 / Selected Record Variab...
Power Apps
Unanswered

Selected Record Variable - Best Practice

(0) ShareShare
ReportReport
Posted on by 103

Hi everyone

 

This is more of a general question as I'm curious how other people lookup records in their apps, for example when passing an item from a gallery to a form.  I've done it two ways and I'm not sure which one is more efficient.  The two ways I've done it are below:

 

1. Set a selected item ID variable e.g. Set(varSelectedItemID, Gallery1.Selected.ID).  Then in the form I would set the Item property as Lookup(ListName, ID = varSelectedItemID).

 

2. Set a selected item variable i.e the whole record e.g. Set(varSelectedItem, Gallery1.Selected).  The form Item property would then be varSelectedItem.

 

I'm starting to lean more towards the second method as it seems a bit more straightforward and also easier to reference other information from the record (sometimes I might want a label on the screen with the record title so I could then just use varSelectedItem.Title for this).

 

One advantage I can see with the first method though is that you always know you'll have the most up to date item details when selecting it from the gallery.  As it's doing a lookup to the datasource it will show the latest version even if another user has changed the item since the app was first opened.

 

I'd be grateful for anyone's thoughts and whether there are any other reasons to use either of the above methods?

Categories:
I have the same question (0)
  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    @gjayne1984 

    Both methods will give equivalent results BUT, If the datasource of the gallery is shaped (like using ShowColumns(), AddColumns(), etc., The Item property of the form will not work as its datasource will not match the datasource of the form exactly.  For this reason, I prefer to use the Lookup(datasource, ID = Gallery1.Selected.ID) as this will still work if the datasource of the gallery it depends on is reshaped.  Whether you use a variable or reference the gallery directly is irrelevant unless you use the same form with 2 different galleries in your app. Then, a variable will be required. 

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @gjayne1984 

    The choice of which to do is usually dependent on the scenario and the design.  There is no particular best practice to that specific situation.

     

    You really need to think in terms of "what data do you already have?"  Consider that your gallery has already taken the performance and datasource hit to get a record (multiple in the case of the gallery).  So, using a Lookup later on to get it yet again, is just a waste. 

     

    There is no sense of getting the most up to date item details as the app has already gotten them and will not get any changes that might have happened since your gallery displayed to the time that you selected it for a form.  You would need to refresh that datasource/record first - which is even more of a performance hit and needs to be designed into your app architecture properly.

     

    However, as mentioned, there are times to do the lookup.  One scenario in mind is when you have data-shaped the table of your gallery.  If you have added rows or done other shaping to it, then the schema of the gallery item will no longer match the schema of the datasource record.  So, either you "un-shape" the record for the form (thus avoiding the datasource re-hit) or you do a lookup again to get the record in the proper schema format.

     

    For the second method - in general, when you are referring to a record, it is always best to set a variable to the full record and not just a column of the record.  Doing so will eliminate having to do even more lookups later on as you will have the full record in the variable.

    One slight modification is to know that ThisItem always refers to the current record, so using Set(varSelectedItem, ThisItem) is sufficient and a little less typing.  It will also adjust for you (from a naming perspective) should you end up doing copies and pastes and the name gets changed.  It is always best to refer to ThisItem, Self, Parent, etc. rather than by name to avoid having to change formulas in the future.

     

    Once additional part of the variable for the record.  It is always good practice to keep the variable up to date.  This is something you have to do always with variables.  They will not change on their own.  So, once you have selected a record in the gallery, edited in a form and then saved, it is best to put the following in the OnSuccess action of the form:  Set(varSelectedItem, Self.LastSubmit)  This way your variable is up to date with the latest changes.

     

    So, to summarize, each of the methods has its purpose and it depends on your overall design and use. 

    One third method you did not discuss is simply referring to the gallery selected item from your form.  Ex: yourGallery.Selected as the Item property.  This involves NO variable.  However, you need to use this only in scenarios where your gallery selections will not change when the form is submitted.  If you have a formula on your Items property of the gallery that involves any data-shaping (including sorting in this case), then your gallery will re-evaluate the formula in the Items and rebuild the gallery every time the datasource changes (as in the submit form).  This will cause the selected item to change to the first one in the gallery.  And, if you are depending on the gallery.Selected record, then it will no longer be what you expect.  BUT, I mention this because, there are times where this is completely usable in a design.

     

    I hope this is helpful for you.

  • Alex123456 Profile Picture
    2 on at

    @RandyHayes Randy Hayes I’m glad you mentioned the third option. I navigated to this thread actually trying to decide if I made the right choice going with option 3 and I believe I did or at least it’ll work. My main data source has a large amount filters, sorts, addcolumns, ect. From there I reference a number of data sources on various screens from the first gallery, while also displaying some general data from the first gallery. In all cases I refer to it as Gallery.Selected. The OnSelect element of that gallery applies an id number filter to the gallery when it navigates to the next screen to ensure that item is always selected so then no matter what data sources I refresh it ensures I always remain on that record. Then you just put for the OnVisible of the main screen to clear the id filter. Just thought I’d throw that out there as an addition to your idea.

     

    but considering what you said about the gallery having to re-evaluate what would you consider the most efficient for an app like I described main gallery has relatively low amount of items but a lot of columns? Just curious

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