Last week, I came across a bug in an app that got me confused for a while. When you set a variable to a collection of records, you might find this behaviour too. At least it is good to be aware of this maybe unexpected behaviour.
The ClearCollect function
Table of Contents
I’m first going to have a look at the ClearCollect function. According to the Microsoft documentation the clear collect does this:
The ClearCollect function deletes all the records from a collection. And then adds a different set of records to the same collection. With a single function, ClearCollect offers the combination of Clear and then Collect.
The Set function
The Set function sounds similar to the ClearCollect
Sets the value of a global variable.
Quick example
I’ve create a SharePoint list so that I can read the items in my datasource from within my Power App.

Nothing complicated. Just a list with the out of the box Title field. Then I added a list item and set the Title to Value.
I’ve added a Datasource for this list in my Power App and my list is now available.

Then I added a button to the app and I’m updating the data using the Patch function. Just to ensure that all is working as expected.

As I’m clicking on the button, I can see that the data in my SharePoint list is updated.

What could possibly go wrong here.
Set vs ClearCollect
I updated the code on my button a but and created one collection using the ClearCollect and one collection using the Set function.

After pressing my button again I can preview my two collections. The collection created with the ClearCollect looks like this:

And the Collection created with the Set function looks like this:

Can you see the difference? Well I can’t, they are the same! Or are they?
Patch after Set vs ClearCollect
Not until after we try to use the Patch function to update these two collections, we will see the difference between Set and ClearCollect.

First I’m going to check the collection Created with ClearCollect. As expected the Title is being updated. The SharePoint list however is unaffected as we are updating the collection only!

The Update on the collection with the Set however is not being updated! As the original value is still there.

And when we look at our data in SharePoint our record is updated.

The explanation
First of all this is not a bug. When we do a Clear Collect we take a copy of the data and create a collection of records separated form the original list.
When we do a Set however we are setting our variable to whatever we supply it with. So in this case we are setting the variable to our list. This explains why the SharePoint list data is updated.
If I now take this to the following code, that is close to the bug that I found in an app last week
ClearCollect(colAnyOddList, 'Any odd list');
Set(colAnyOddList3, Filter('Any odd list', ID = 1));
Patch(colAnyOddList, LookUp(colAnyOddList, ID = 1), {Title : "Updated Again"});
Patch(colAnyOddList3, LookUp(colAnyOddList3, ID = 1), {Title : "Updated Again 2"}); In this case the Patch function on the Set function that takes the output from the Filter action will not update anything in my colAnyOddList3 variable. However the SharePoint list does get updated again.
So this means that ClearCollect creates a copy, while Set creates a reference to the datasource. This of course could result in a small performance improvement, however in general I would probably avoid this use of the Set function as it makes code confusing.
Discover more from SharePains
Subscribe to get the latest posts sent to your email.
I would recommend using ClearCollect to store tables and Set to store single (global) variables. This way it’s not confusing and you avoid heavy troubleshooting. Nice catch though!
I totally agree. ClearCollect is clearer.