cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Dlabar
Level 8

How Do Variables References Work In Canvas Apps?

I would assume that these two functions would be equivalent:

Function 1

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

Collect(Collection,newItem);
Patch(Collection, newItem, { qty:1 } );

Function 2

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

Patch(newItem, { qty:1 } );
Collect(Collection,newItem);

But I'm finding that they are not.  I would expect objects to always be "passed by reference" but do fomulas in Canvas Apps always "pass values by value"?

1 ACCEPTED SOLUTION

Accepted Solutions
Dlabar
Level 8

Re: How Do Variables References Work In Canvas Apps?


@CarlosFigueira wrote:

The two expressions are roughly equivalent, with an important distinction: the Patch function when used without a collection (in the second expression) "returns the merged record and doesn't modify its arguments or records in any data sources" (from the Patch documentation). If you want both expressions to be equivalent, you can use the following modified expression:

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

UpdateContext({newItem: Patch(newItem, { qty:1 } )});
Collect(Collection,newItem);

Hope this helps!


Actually, your updated function 2, is still not equivelent to function 1.  Patch with a data source doesn't update the actual item in the collection, it creates a new item from the patch, and uses that.  So for example the following function updates the newItem in the collection to have a a qty of 1, and then the second patch creates a new item, with no name and a qty of two, since the newItem variable, is no longer in the collection.

 

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

Collect(Collection,newItem);
Patch(Collection, newItem, { qty:1 });
Patch(Collection, newItem, { qty:2 });

So for the desired equivilent functions would look like this:

Function 1

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

Collect(Collection,newItem);
UpdateContext({ newItem: {Patch(Collection, newItem, { qty:1 } )});

Function 2 (your function)

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

UpdateContext({newItem: Patch(newItem, { qty:1 } )});
Collect(Collection,newItem);

 

View solution in original post

3 REPLIES 3
Power Apps Staff CarlosFigueira
Power Apps Staff

Re: How Do Variables References Work In Canvas Apps?

The two expressions are roughly equivalent, with an important distinction: the Patch function when used without a collection (in the second expression) "returns the merged record and doesn't modify its arguments or records in any data sources" (from the Patch documentation). If you want both expressions to be equivalent, you can use the following modified expression:

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

UpdateContext({newItem: Patch(newItem, { qty:1 } )});
Collect(Collection,newItem);

Hope this helps!

Dlabar
Level 8

Re: How Do Variables References Work In Canvas Apps?


@CarlosFigueira wrote:

The two expressions are roughly equivalent, with an important distinction: the Patch function when used without a collection (in the second expression) "returns the merged record and doesn't modify its arguments or records in any data sources" (from the Patch documentation). If you want both expressions to be equivalent, you can use the following modified expression:

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

UpdateContext({newItem: Patch(newItem, { qty:1 } )});
Collect(Collection,newItem);

Hope this helps!


Actually, your updated function 2, is still not equivelent to function 1.  Patch with a data source doesn't update the actual item in the collection, it creates a new item from the patch, and uses that.  So for example the following function updates the newItem in the collection to have a a qty of 1, and then the second patch creates a new item, with no name and a qty of two, since the newItem variable, is no longer in the collection.

 

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

Collect(Collection,newItem);
Patch(Collection, newItem, { qty:1 });
Patch(Collection, newItem, { qty:2 });

So for the desired equivilent functions would look like this:

Function 1

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

Collect(Collection,newItem);
UpdateContext({ newItem: {Patch(Collection, newItem, { qty:1 } )});

Function 2 (your function)

UpdateContext({ newItem: { name: Text(CountRows(Collection)+1), qty: 0 }});

UpdateContext({newItem: Patch(newItem, { qty:1 } )});
Collect(Collection,newItem);

 

View solution in original post

Power Apps Staff CarlosFigueira
Power Apps Staff

Re: How Do Variables References Work In Canvas Apps?

Ah, I think understand your question now. Variables are added to collections in PowerApps "by value", not "by reference" - they are cloned and then added to the collection. If you add a variable to a collection, then modify that variable, the modification will not be shown in the collection.

Helpful resources

Announcements
Better Together’ Contest Finalists Announced!

'Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Join THE global Microsoft Power Platform event series

Attend for two days of expert-led learning and innovation on topics like AI and Analytics, powered by Dynamic Communities

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

thirdimage

Microsoft Business Applications Virtual Launch

Join us for the Microsoft Business Applications Virtual Launch Event on Thursday, April 2, 2020, at 8:00 AM PST.

thirdimage

Community Summit North America

Innovate, Collaborate, Grow - The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (10,275)