Collection Assignment Operators in Flow

Collection Assignment Operators in Flow

Variable is a place holder that stores a value of a specific data type, which can be used or changed throughout the flow. It can store a single value or it can be configured as a collection, which makes it an ordered list of a specific data type. The value that it stores can be easily changed using the assignment element. When using the assignment element, operators depend on the type of the variable. For instance, if it is a text variable, only "Equals" and "Add" operators are available. On the other hand, if it is a collection variable, you will see more operator options. There are many assignment operators that you can use with collection variables. Although "Equals" and "Add" operators are the most common ones, there are many useful collection assignment operators in Salesforce Flow.

Collection Assignment Operators

Let's say that there are two text collection variables in a screen flow. The first one stores the names of the test accounts. The second one stores the names of the accounts that contain "Account".

Initial collections

Now, let's see how to use the collection assignment operators.

1- Equals

Equals operator replaces the value of the Variable. You have to select a collection of the same data type or object type. If you are working with a record collection, it is also possible to select a single record variable as the Value.

In this case, equals operator makes the FirstCollection equal to the SecondCollection.

Assignment Operators: Equals operator
Result of the Equals operator

As you can see, value of the FirstCollection is equal to the SecondCollection.

2- Add

Add operator adds the Value as a new item at the end of the collection in Variable. Just like the Equals operator, you have to select a collection of the same data type or object type. If you are working with a record collection, you can also select a single record variable as the Value.

In this case, Add operator adds the SecondCollection to the end of the FirstCollection.

Assignment Operators: Add Operator
Result of the Add operator

3- Remove First

Remove First operator removes the first instance of the Value from the selected collection in Variable. For the Value, you have to select a single variable of the same data type.

Let's assume that SingleAccountName is a text variable that stores "Test2". In this case, it removes this value from the FirstCollection.

Assignment Operators: Remove First Operator
Result of the Remove First operator

As you can see, FirstCollection doesn't contain "Test2" anymore.

4- Remove Before First

Remove Before First operator finds the first instance of the Value in the selected collection. Then it removes all the items before that first instance. For the Value, just like the Remove First operator, you have to select a single variable of the same data type.

In this case, it removes all the values before "Test2".

Assignment Operators: Remove Before First Operator
Result of the Remove Before First operator

As you can see, all the values before "Test2" are removed from the FirstCollection.

5- Remove After First

This is the opposite of the Remove Before First operator. It finds the first instance of the Value in the selected collection. Then it removes all the items after that first instance. For the Value, just like the Remove Before First operator, you have to select a single variable of the same data type.

In this case, it removes all the values after "Test2".

Assignment Operators: Remove After First Operator
Result of the Remove After First operator

As you can see, all the values after "Test2" are removed from the FirstCollection.

6- Remove All

Remove All operator removes all instances of the Value from the selected collection in Variable. For the Value, you can select a collection of the same data type or object type. You can also select a variable of the same data type or record variable of the same object type.

Just for this example, let's double the values of the FirstCollection. As you can see, it contains "Test Account1" and "Test Account2" twice.

Values of the collections

Now, let's remove all the values of the SecondCollection from the FirstCollection.

Assignment Operators: Remove All Operator
Result of the Remove All operator

As you can see, "Test Account1" and "Test Account2" are removed from the FirstCollection.

7- Add At Start

Add At Start operator adds the Value as a new item at the beginning of the collection in Variable. It is just like the Add operator, but it adds the Value to the beginning. For the Value, you can select a collection of the same data type or object type. Or you can also select a variable of the same data type or record variable of the same object type.

In this case, it adds the values of the SecondCollection to the start of the FirstCollection.

Assignment Operators: Add At Start Operator
Result of the Add At Start operator

8- Remove Uncommon

Remove Uncommon operator finds the items in the Value collection within the Variable collection. Then it keeps the found items, and removes all other items from the collection in Variable. For the Value, you have to select a collection of the same data type or object type.

Before doing this example, let's add "Yumi" to the SecondCollection. As you can see, "Test Account1" and "Test Account2" are the common values.

Values of the collections

In this case, Remove Uncommon operator removes the rest of the values from the FirstCollection.

Assignment Operators: Remove Uncommon Operator
Result of the Remove Uncommon operator

9- Remove Position

Collections store multiple values of the same data type or records of the same object in an ordered list. It means that each item in a collection has a position. You have to enter a number for the Value. It specifies a position in the collection. Remove Position operator removes the item at the position from the collection in Variable.

In our example, FirstCollection has 7 items and the 3rd one is "Test1".

Assignment Operators: Remove Position Operator
Result of the Remove Position operator

As you can see, 3rd item ("Test1") is removed from the FirstCollection.

There is something that you have to pay attention here. If the collection has less items than the number that you entered, it cannot remove the item. In this case, flow fails.

For example, FirstCollection has 7 items. If you use the Remove Position operator and enter 10 for the Value, flow fails with this error:

Error element Collection_Assignment (FlowAssignment).
The "Collection_Assignment" element can't remove the item in position 10 of the "FirstCollection" collection variable because the collection contains only 7 items.

Summary

There are many assignment operators that you can use for collections. Although "Add" and "Equals" operators are the ones that you need almost in any flow, the remaining operators can be very useful as well. It is important to understand how to use them. In some cases, they might make your flows less complicated and perform faster.

3 Comments

Leave a Reply

Your email address will not be published.


*