How to Use the In and Not In Operators In Flow

How to Use the In and Not In Operators in Flow

Salesforce introduced two new operators in the Winter ’23 release. Using the In and Not In operators, you can get related records without using a loop. These operators make the flow use less SOQL queries and DML statements. Consequently, they ensure your flow doesn’t exceed governor limits and increase its performance. It is possible to use the In and Not In operators in Get Records, Update Records, and Delete Records elements.

Using these operators, it is possible to filter data according to a value in (or not in) a collection. Therefore, before using these operators, you should have a collection that stores the values. In and Not In operators can access collections of type Text, Number, Date, Date/Time, Currency, and Boolean. Although these are the supported collection types, in most of the cases, you will need a text collection.

For example, if you have a text collection of Ids, you had to loop through them and use a Get Records element to query each one of those records. However, since it involves a SOQL in a loop, it was never recommended. Therefore, you had to use some custom actions. Starting with the Winter ’23 release, you can use the In and Not In operators in a Get Records element. So that you don’t need to perform SOQL in a loop or use custom actions.

Here is a simple example using the standard objects of Salesforce. Let’s assume that you want to get the Closed Won opportunities for all accounts with open cases.

How to Use the In Operator

1- First of all, add a Get Records element to get all the open cases. Choose to store all the case records that meet this criteria. This will create a record collection variable.

Get Open Cases

2- Add a loop element to iterate over Case records from the record collection.

Loop through cases

3- Create a text collection called AccountIds and add an Assignment element. Add the AccountId value of the current item to the AccountIds collection. This assignment should be in the loop that you added in the previous step.

Assignment element to add account Ids to a collection

4- At the end of the loop, you will have a text collection that stores the Ids of the account records. Now that you know the account Ids, it is time to get the Closed Won opportunities related to those account records.

Add a Get Records element to get the opportunities. Here is how you can use the In operator.

Using In and Not In Operators in a Get Records element

At the end, your flow should look like this. The last screen element is optional, you can use anything that you want.

End of the flow using the In and Not In operators

There are many open cases, so the AccountIds text collection contains lots of Ids. However, using the In operator and another criteria for the Stage, flow found only 2 opportunities. As you can see, their accounts are different.

Result of the flow

Summary

In and Not In operators are great features that Salesforce released in the Winter ’23 release. Using these operators, your flows will perform better and you will need less elements. In this post we saw a simple example but it is possible to use these operators with more complex logic. On the other hand, you can use these operators with the Update Records and Delete Records as well.

1 Comment

Leave a Reply

Your email address will not be published.


*