cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BHS_KI
Helper II
Helper II

Gallery: New Item record and show it selected fails (Timing Issue)

Hi there,

 

I have a problem when posting a new record into a gallery and want to select it in the gallery afterwards. I use VarRecordS to keep the default Record. I want to update it after posting. I think there is a big timing issue, meaning command No. 3 is executed quicker than command No. 1. In my example I consider a new entry always as the last entry in the gallery.

 

My coding looks like:

Patch(Material_Project;Defaults(Material_Project);{MaterialID:Cb_Material_1.Selected.MatID};{ProjectID:glbProject.ProjectNo};{MaterialName_Project:Ti_PName_1.Text}; {Comment: Ti_Mcomment.Text});
Refresh(View_ProjectMaterial);
UpdateContext({VarRecordS: Last(Gallery.AllItems)});
 
1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@BHS_KI 

No, timers are not reliable.  How do you know how long to set it? Could take a few seconds or minutes to refresh!

 

Okay...so a dynamic table variable.  Here is what you do.

 

There is a little known and rarely used property in PowerApps on a datacard.  It is called Update.  Now, when I refer to a datacard, I'm not referring to one that is in a form, but one that is in a Canvas.

 

You cannot add a canvas or a datacard to your screen.  You have to "bring" it to the screen.

 

So, here is what you do:

- First, add a new screen to your app and choose to add a Scrollable screen.

 

- You will now have a new screen that has a couple things in it.  The one we are interested in is the Canvas control.  You will click on that and then resize the entire canvas to a small size (we don't really need it to display anywhere, we just need the control, so it can be non-visible where it is going).  Once that is resized, you will Cut (ctrl-x) the control from the screen.  You can then delete the screen as well as we don't need it.

 

- Now, back on your other screen, paste that canvas control (ctrl-v) And now you have brought the control to your screen.  Let's work with it!

 

- In the canvas control will be a datacard.  Again, we don't care what it looks like as we are not putting any visible controls in it.  BUT, we are interested in the Update property of that Datacard.

 

- The Update property of that datacard is a Record.  It can be ANYTHING you want!  All you have to do is define your record there.

{
    myItems: yourDataSource/Filter/orAnythingElse
}

 

- Now, you have a record that will always change when any value in it changes.

- You can reference this just like any other record.

 

SO, in your case, you would replace the "yourDataSource" part in the Update formula to be your view

 

And now, this is a table for you to use.

So you can set your Default property of the Gallery to: LookUp(DataCard1.Update.myItems, ID=yourPatchedID)

This will then all dynamically change once the refresh completes.

 

These type of dynamic record and table variables really change things!!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

7 REPLIES 7
RandyHayes
Super User
Super User

@BHS_KI 

Please consider changing your Formula to the following:

UpdateContext({VarRecordS:
    Patch(Material_Project;
        Defaults(Material_Project);
        {MaterialID:Cb_Material_1.Selected.MatID;
         ProjectID:glbProject.ProjectNo;
         MaterialName_Project:Ti_PName_1.Text; 
         Comment: Ti_Mcomment.Text
        }
    )
})

Avoid doing a Refresh - you don't need it and it will only slow down your app.

Patch returns the entire record that is patched...so in the above, just set it to your context variable.

 

I hope this is helpful for you. 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Thanks for your reply. This does not work. Because I use a SQL-View in the Gallery and a Patch command in the associated table. 

RandyHayes
Super User
Super User

@BHS_KI 

Missed that your refresh was for a view!  Views are good and bad.  Good that you can predefine the data that comes to your app...bad because they are not dynamic like a regular table-based datasource.  So, you have to incur the performance hits all the time to refresh the views.

This can be worked around in the app to some degree, but you end up writing more formulas to maintain on both sides.

 

So, back to your original post - yes, Refresh will be asynchronously performed.  So, Getting the Last record of the AllItems on the Gallery will be incorrect as the Gallery will not (most likely) have updated by the time your line of the formula is evaluated for the UpdateContext.

 

I would look at this from a couple of ways...your Patch is going to return the record that was patched.  Is the primary key or some other identifying value returned in the view as well?

If so, then you can move the Items property of your gallery out to a dynamic table variable (ask more - as this is a concept that very few know). Then have your Gallery Items come from that table and then have your Default do a LookUp on that table as well with the ID (or other identifying column) based on the Patched record.  Once the view refreshes, the LookUp will return a valid record and the gallery will change its selection accordingly.

The other advantage of that is - you're currently assuming the last record is the new one - if you change sorting or other aspects of your gallery, you will still be then able to select the correct record, regardless of where it is.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Thanks Randy,

your idea with "dynamic table variable" sounds interesting. I could link returning key from the Patch command to the view. But the rest of the logic is not clear to me. You already mentioned just a few know. So not me. 😇 

And I do not want to play with timers. 

RandyHayes
Super User
Super User

@BHS_KI 

No, timers are not reliable.  How do you know how long to set it? Could take a few seconds or minutes to refresh!

 

Okay...so a dynamic table variable.  Here is what you do.

 

There is a little known and rarely used property in PowerApps on a datacard.  It is called Update.  Now, when I refer to a datacard, I'm not referring to one that is in a form, but one that is in a Canvas.

 

You cannot add a canvas or a datacard to your screen.  You have to "bring" it to the screen.

 

So, here is what you do:

- First, add a new screen to your app and choose to add a Scrollable screen.

 

- You will now have a new screen that has a couple things in it.  The one we are interested in is the Canvas control.  You will click on that and then resize the entire canvas to a small size (we don't really need it to display anywhere, we just need the control, so it can be non-visible where it is going).  Once that is resized, you will Cut (ctrl-x) the control from the screen.  You can then delete the screen as well as we don't need it.

 

- Now, back on your other screen, paste that canvas control (ctrl-v) And now you have brought the control to your screen.  Let's work with it!

 

- In the canvas control will be a datacard.  Again, we don't care what it looks like as we are not putting any visible controls in it.  BUT, we are interested in the Update property of that Datacard.

 

- The Update property of that datacard is a Record.  It can be ANYTHING you want!  All you have to do is define your record there.

{
    myItems: yourDataSource/Filter/orAnythingElse
}

 

- Now, you have a record that will always change when any value in it changes.

- You can reference this just like any other record.

 

SO, in your case, you would replace the "yourDataSource" part in the Update formula to be your view

 

And now, this is a table for you to use.

So you can set your Default property of the Gallery to: LookUp(DataCard1.Update.myItems, ID=yourPatchedID)

This will then all dynamically change once the refresh completes.

 

These type of dynamic record and table variables really change things!!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Hi Randy,

 

thank you so much for your great help.

Unfortunately I do not really get the logic assigned to my use case.

 

I have:

1) Gallery with the a DataSource as a View. The view is filtered to a master table. I use a varRecord for showing the row selected.

2) A popup container for adding a new entry on the sourcing table of the view. I do not use a Form with a DataCards. Just some input text controls for my patch command to post a new record. 

When I leave the PopUp after my patch command I wanted to show my new record in the gallery and make it selected (via varRecord). 

 

I do not understand how to synchronize the Update property of the new canvas control to my workflow. 

BHS_KI
Helper II
Helper II

Now I succeed in your method to synchronize the updates. Thanks a lot. 

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (1,680)