Power Apps: Default item of Gallery after Form Submit

Why?

When you have a Gallery linked to a data source and submit a form: the data source refreshes itself. Usually this is exactly wat you want because the changes of the last submitted form should be reflected in the Gallery: however the refresh also results in the Gallery being refreshed making sure that your selected item in the gallery is unselected or even changed to the first item on top… ๐Ÿคจ

What?

This blog post will give a simple solution to make sure that after a form is submitted, the default selected item of the gallery is that same item that was just submitted.

How?

So below a Power Apps Screen with on the left side a Gallery Control called Gallery_Contacts and on the right side a Form Control called Form_Contacts_edit.

The default item of the Form Control is set on Gallery_Contacts.Selected making sure that the selected item in the Gallery Control is loaded in the Form Control immediately.

1) In the OnSuccess Propery of Form_Contacts_edit we will add the formula:

UpdateContext({varFormLastSubmit:Form_Contacts_edit.LastSubmit})

This will update the variable varFormLastSubmit with all the data of the last submitted item of this Form control.

TIP: Looking at the View tab of the Power Apps Studio, you can select the Vaiables button to view your variables and see what data is currently in your variable ๐Ÿ’ก

2) At last we just set the Default Property of Gallery_Contacts to varFormLastSubmit and that is it!

Please note that you could also use the formula of Form_Contacts_edit.LastSubmit directly without the use of a context variable. I however chose to use a variable:

  • for troubleshooting purposes
    and even more important:
  • for a new item in the same datasource and thus in the same gallery, I have added a second Form Control and with adding the same OnSuccess Property formula in this second control referencing the same variable โžก the Gallery will be selecting the new item immediately after submitting no matter which Form Control was being used last ๐Ÿ˜Ž

12 thoughts on “Power Apps: Default item of Gallery after Form Submit

  1. Thanks for this post – I did have some issues getting it to work though. When the form submitted, the gallery was not selecting anything. I have discovered that this method has issues if you are using the Search() function around the data source of the gallery. As soon as I removed the Search function it worked as expected. I also use Filters() and Sort() on the data source and these are OK.

  2. Hi Django,
    I did follow the same steps but it is not working for me. Can you please help me with this?

      1. Hi Django,

        I have posted the question in the Community forum and I have tagged you.
        Please help me with the solution as soon as possible.

        Thanks in advance.

        1. Hi Navya,
          I did not receive any notification. Maybe something wrong in the forum or maybe another Django was tagged ๐Ÿ˜
          You can also share the link to the poste here.

  3. Django! Just wanted to say a massive thank you for fixing this issue for me. #You’re a pro at PowerApps and I don’t think I could have fixed it myself. So thank you again ๐Ÿ™‚

  4. Thank you Django for this post! When I try doing the same thing (I’ve got three galleries, gallery 1 filters gallery 2 and gallery 2 filters gallery 3 and the item from gallery 3 is displayed in a form). When I try doing this it gives me an error saying “Expected a value compatible with items”. Not sure why

    1. Hi Bilal,
      Are you using the UpdateContext({varFormLastSubmit:Form_Contacts_edit.LastSubmit}) construction? Because the Items property of the Gallery Control expects a record data type, you need to make sure that the variable you are using after successfuly submitting the form is also of the data type record (and not another data type like text or table).

      1. Hi Django. Sorry i’m kind of new to Powerapps and especially with regards to using variables. Yes I’m using the update context as per your blogpost for the Onsuccess property of my form. The items property of my gallery which is linked with the form is GroupBy(Filter(Table2,Areas=Gallery3_4.Selected.Areas),”Questions”,”Response1″,”Response2″,”Riskarea”) where Areas, Questions, Response1 and Response 2 are my columns and Table 2 is my data source. When I use the varformlastsubmit variable in the default property of my gallery I get this error. I’m not sure what the record type is that I need to be using. Any ideas?

        1. No problem! We were all beginners at the start ๐Ÿ˜‰
          I added a TIP in the post above so you can check if your variable has a value. If it has a value, it should be a Record because the Form_Contacts_edit.LastSubmit formula which you store in the variable is the last record you submitted using that form.
          Did you also try to use Form_Contacts_edit.LastSubmit formula directly to see if the issue is with the variable?
          Please note that before this formula can have a value –> the form has to be submitted at least once otherwise there is no data. If needed we can have a short call? Using Microsoft Teams we can have a call and maybe I can help and even improve my blog post afterwards. ๐Ÿ‘

          1. Hi Django. Thank you so much for your responses. I did check and there appears to have a record value in view–>variables. A call will be great. Happy to work around your schedule and thanks so much for the offer.

Leave a comment