2

TLDR: If you have two record collection variables that have the exact same records, in the same order, how can removing one from the other NOT result in a blank collection?

Full question with background: I have a custom object named Board, with two custom lookup fields to a contact record: Primary__c and Secondary__c. These contacts listed here correspond to community users, so each contacts is associated to a user record via the user record's ContactId field.

I have a screen flow that allows an internal user to change the contacts for a board record. The contacts may be changed in any number of ways, or not changed at all. The flow queries the original users associated with the contacts (sorted by created date, descending) and stores results in a record collection variable called "originalBoardUsers". Then, after the contacts for the board are updated, the flow queries the user records associated with the new contacts with get records element "GetCurrentBoardUsers" (again sorted by created date, descending) and populates a record collection variable called "currentBoardUsers". It is possible that the contacts were not changed by the running user, in which case the values for the two collections should be the exact same.

In the next step, an assignment element works to calculate the users that were removed and those that were added, with the following (among other things):

  • currentBoardUsers - REMOVE ALL - originalBoardUsers
  • originalBoardUsers - REMOVE ALL - GetCurrentBoardUsers ***

I am using the GetCurrentBoardUsers to update the originalBoardUsers collection since currentBoardUsers is now different than its starting value, due to the prior assignment

Here is the problem: If the user does not change the contacts, then theoretically, removing original from current should result in a blank collection, right? But it doesn't. For some reason, the REMOVE ALL operator leaves only one of the two values in the collection, even though both values are the same.

So the question is: What is going on? How can I make equal collections "cancel each other out"?

(screenshots below. Ignore the "removed" and "added" variables for now)

enter image description here enter image description here enter image description here

1 Answer 1

3

tl;dr: Remove All checks all fields in each record when trying to remove records from a collection. Make sure you're retrieving/setting the same values for every field.


You might be tempted to think that the Remove All operator looks at just the ID field to determine if there's a matching value, but this is untrue. All fields in the record are considered for equality, and subsequent removal.

To demonstrate, I create two accounts in memory, set field values, assign each record to a collection, and try to remove the account from the first collection:

Assignment element setting values in two accounts, assigning each to a separate collection, then attempting to remove the first account with the second account.

When we run this, we get this output:

Debug result showing that the account in Collection1 is still present.

As you can see, the account wasn't removed. However, if we make the accounts equal to each other value for value:

We create two accounts in memory, each with the Name and Account Number set to the same values. We then add each account to a separate collection, and attempt to remove the values in Collection2 from Collection1.

We now get a different debug output:

Debug results showing that Collection1 no longer has an account it.

Therefore, make certain that the records you retrieve from various sources have the exact same fields, otherwise the Remove All operator may not work as you expect.

I performed a cursory search on the Internet, and this behavior doesn't seem to be specifically documented. People have asked about it before, but it doesn't seem to be common knowledge as of yet.

2
  • Wow. There are so many intricacies in SF that would be so hard to realize/find out if it weren't for people like you sharing their knowledge. This is exactly what the issue was. I changed my Get Records elements to store only the ID of the user records so the user collections would be compared ONLY the Id valuer, and then the two collections successfully canceled each other out, as intended. Thanks so much!! Commented Apr 22 at 16:42
  • @MiguelDiaz You're welcome! I don't use flows much, so I actually had to experiment with it. I learned something new that day as well.
    – sfdcfox
    Commented Apr 22 at 18:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .