cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Oskarkuus
Post Prodigy
Post Prodigy

Powerapps - best practice on using patch?

I have just spent quite some time searching for a strange error in my app.

 

When i press a button i run two patch functions.

patch 1 updated table 1 data and patch 2 updated table 2 data.

table 1 and table 2 have an relationship.

 

in my app when i press a row in a gallery i do set "thisItem" to an variable.

set(varThisItem;gallery1.selected)

Then i navigate to a new screen

on this screen i have some textboxes, comboboxes that i populate using

varThisItem.column1 etc.

 

then to save the data from the textboxes and comboboxes i use patch to update some of the table1 columns.

the other patch function that update table2 do so just to create a new record and log that a change have been made by a user.

 

but for some reason som lookup columns is set to blank. i could not figure out why.

 

but i solved this by separating the code and first run the patch on table1 then i added a second button and auto-pressed it with select(button2) and then run the patch for table2.

 

i cant explain why i got it to work.

it feels like the code work to fast so the datasource does not have time to update.... by separating the code i guess i get some delay.

can this be true?

 

i have noticed that i do benefit from adding some timers and to delay the code sometimes.

but i can not figure out when i SHOULD use it and when i dont need to....

 

looking for some kind of explanation to better understand why i need so many controls in my apps just to have them hidden and run code from...

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @Oskarkuus ,

 

Yes, it will be the id value of the newly created row in datasource1. Since the Patch function is in the variable, you will have the Patch function fired and get the returned result being saved into the variable. If you make a bulk update using ForAll/Patch or UpdateIf, you can even append it into Collect/ClearCollect functions to create a collection with all the updated/created records.

 

Best regards,

Community Support Team _ Jeffer Ni
If this post helps, then please consider Accept it as the solution to help the other members find it.

Community Support Team _ Jeffer Ni

If this post helps, then please consider Accept it as the solution to help the other members find it.

View solution in original post

4 REPLIES 4
v-jefferni
Community Support
Community Support

Hi @Oskarkuus ,

 

Your suspicion is basically right on the data source delay. The reason is from the LookUp columns you mentioned, they will rely on the records having created successfully within the main source. Using the Timer control will normally around this problem but like you said there will be too many hidden controls and make app makers unhappy.

 

There is another workaround, you can try and use a variable to save the result of the first Patch and use it for the second one:

Set(varPID, Patch(DataSource1, Defaults(DataSource1),{...}).ID);
Patch(DataSource2,Defaults(DataSource2),{LookUpColumn:LookUp(Choices(DataSource2.LookUpColumn),ID = varPID)})

 

Hope this helps.

 

Best regards,

Community Support Team _ Jeffer Ni
If this post helps, then please consider Accept it as the solution to help the other members find it.

Community Support Team _ Jeffer Ni

If this post helps, then please consider Accept it as the solution to help the other members find it.

hm that was news to me.

so do i understand you correct, you mean that i can save a patch functions result in a variable?

Set(varPID, Patch(DataSource1, Defaults(DataSource1),{...}).ID);

will this variable be the id-value of the newly created row in datasource1?
but wont i need to run this patch aswell so it actually creates the new row?

 

 

 

 

Hi @Oskarkuus ,

 

Yes, it will be the id value of the newly created row in datasource1. Since the Patch function is in the variable, you will have the Patch function fired and get the returned result being saved into the variable. If you make a bulk update using ForAll/Patch or UpdateIf, you can even append it into Collect/ClearCollect functions to create a collection with all the updated/created records.

 

Best regards,

Community Support Team _ Jeffer Ni
If this post helps, then please consider Accept it as the solution to help the other members find it.

Community Support Team _ Jeffer Ni

If this post helps, then please consider Accept it as the solution to help the other members find it.

Great

Did not know i could fire a patch as i was swrring a variable and get the id from the patch.

 

This solves a few things by removing timers and buttons to call. 

 

 

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Top Solution Authors
Top Kudoed Authors
Users online (2,485)