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

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: Powered On

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
PowerApps Staff CarlosFigueira
PowerApps 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: Powered On

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

PowerApps Staff CarlosFigueira
PowerApps 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
thirdimage

Power Automate Community User Group Member Badge

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

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors (Last 30 Days)
Users online (4,740)