Use In and Not In Operators in Salesforce Flow

In Salesforce Flow, we use the Get Record element to retrieve records from an object. When we store those records as a collection, there will be two options for the records:

  1. Either we want to use records from that collection
  2. Or we do not want the records to be from that collection

For this, we have the In and Not In operators in Salesforce Flow, which we can use to perform operations on the records in the collection variable.

In this article, I will explain how to use “In” and “Not In” operators in Salesforce Flow. In this, we will learn where it can be used and how to use it, with real-time examples and step-by-step explanations.

What are In and Not In Operators in Salesforce Flow?

Using the ‘In’ and ‘Not In’ operators in Salesforce Flow, we can easily query the related data without writing a query inside the loop element. Use the In operator to ensure that your flows do not exceed governor limits.

The new operators allow you to access collections of the following types: Text, Number, Date, Date/Time, Currency, and Boolean. The operators are available in the elements “Get Records”, “Update Records”, and “Delete Records”.

Example 1: Use the In Operator in Salesforce Flow

Below, I will explain the use case and demonstrate how to use the In operator in Salesforce Flow to access records from a list (collection variable).

A sales company wanted to automate the process of sending automated renewal contract reminder emails 30 days before the contract expiration to their account holders. However, only those with a subscription type of either Diamond or Platinum are eligible.

To send a reminder email 30 days before contract expiration, we need to check daily if any contracts will expire. For this, we will use the Schedule Trigger Flow.

  • Navigate to the setup, and in the Quick Find box, search for ‘Flows’ under Process Automation.
    • In the flows setup window, click on the button New Flow. Select the option Schedule-Triggered Flow, and click Create.
    • Select the Start Date and Start Time to schedule the flow. After this, select the frequency from the options: OnceDaily, or Weekly.

Next, we need to retrieve all the accounts stored in the Account object. For that, we need to add the Get Records element and enter a Label and API Name.

Then, to filter the account records, add a condition. Here, I set the Subscription Type equal to ‘Diamond’ or ‘Platinum’.

Due to these filter conditions, we retrieved only those records that met the defined criteria. Then we need to store ‘All Records‘ as a collection.

Use IN operator in Salesforce Flow to Filter Records

Now we want to store all the retrieved account IDs in a variable. For that, we need to iterate over all account records and retrieve the account ID.

We will use the loop element to iterate over the records retrieved in the ‘Get Record’ element. To add the Loop Element, click on the Add Element icon.

Then, enter the element Label and API Name.

Then, select the Collection Variable from Get Records(API Name) and select the iteration direction from the first item to the last item.

Example of NOT IN operator in Salesforce Flow

To store the Account IDs, we need to create a new variable. To do that, click New Resource and select the Resource Type as Variable. Enter API Name. Here, I have entered DiamondPlatinumPlanIds.

Select ‘Data Type’ as ‘Text‘ and allow multiple values, and click on Done.

Use IN operator in Salesforce Flow

In this step, we will add the ‘Account ID’ from the current record of the loop element. To do so, add the Assignment Element. Then, enter the element Label and API Name.

Here in the Set variable Values, I added a condition as follows:

DiamondPlatinumPlanIds -> Add -> Loop > Account ID: Here, we store the account ID in the created collection variable.

Filter related records using IN in Salesforce Flow

Next, we need to retrieve all contracts whose contract end date falls within the next 30 days, stored in the Contract object. For that, we need to add the Get Records element and enter a Label and API Name.

Now we will filter the conditions to retrieve only records that meet the defined criteria.

Here, I added two conditions as follows:

  • Contract End Date -> Equals ->TODAY()+30 (Formula Resource).
  • Account ID ->IN -> DiamondPlatinumPlanIds (Text Variable).

Here, we also need to select the All Records option to store all account IDs and contract end date records in the collection.

Use In and Not In Operators in Salesforce Flow

Again, add the Loop element and select the above get-record element API Name, which stores all contract and account IDs.

Salesforce Flow clean conditions with NOT IN filter

Now, again, we need to add the Assignment element where we store the accounts so we can send an email to an account.

This ensures that we receive only accounts with contracts that have upcoming renewals within 30 days and those with Diamond or Platinum plans.

IN vs NOT IN operators explained visually in Salesforce Flow

After that, we would like to send an email to the accounts to notify them that their contract will expire in 30 days. For that, add the Send Email action.

Here, you need to select the field values to send an email, such as sender email, subject, body, recipient address, etc.

In the body, we inform the account holder that their contract will expire in 30 days. Whatever you want to send as an email template, you can send.

Bulk update records with IN operator in Salesforce Flow

Now we are ready to save the flow. For that, click the Save button, provide the flow Label, and the API Name will be automatically populated.

After that, always debug the flow before activating it to ensure that the working flow is correct and that there are no runtime errors. Then activate the flow.

Using IN operator to include records in Salesforce Flow
Using NOT IN operator to exclude records in Salesforce Flow

Proof of Concept:

Below, you can see that I received an email with the contract number and expiration date. These emails will now only be sent to accounts with a subscription type of Diamond or Platinum, and their contracts will expire within the next 30 days.

Salesforce Flow scenario using IN and NOT IN together

In this way, we can use the In operator in Salesforce Flow to access records from a list.

Example 2: Use the Not In Operator in Salesforce Flow

Now, I will explain how to use the ‘Not In’ operator in Salesforce Flow with picklist values to check if a value is not included in a list of values. (collection variable).

A company wanted to automatically delete Leads if they were older than 1 year, but to skip Leads that came from priority sources, such as ‘Outbound Calls’ and ‘Partner Referral.’

To delete leads older than 1 year, we need to check the creation date so we can schedule a trigger flow.

Set the start element according to your requirements.

Next, add a Get Record element to retrieve the leads that are older than 1 year. For that, I added the following condition:

  • Created Date > Less Than > TODAY() – 365 (Formula Resource).

After that, we also only want those leads whose sources are ‘Outbound Calls’ and ‘Partner Referral.’

After adding the conditions, select the “Store All Records” option so it functions as a collection variable (list).

Using collection variables with Not IN in Salesforce Flow

Then create a new resource type called ‘Variable‘ with a data type of ‘Record‘ and select the ‘Lead‘ object, allowing multiple values to store lead IDs.

Salesforce Flow filter Records using Not IN operator

Now, add the Loop element and select the above ‘Get Record‘ element API Name, where we have stored all leads.

Now, we will add the ‘Leads IDs’ from the current record of the loop element. To do so, add the Assignment Element. Then, enter the element Label and API Name.

Here in the Set variable Values, I added a condition as follows:

PriorityLeadSourcesCollections -> Add -> Loop > Lead ID: Here, we store the lead ID in the created collection variable.

Salesforce Flow exclude high-value records using NOT IN

After that, add a Decision element to check whether the lead IDs we added to the record collection variable in the assignment element are available.

IN operator filtering records in Salesforce Flow

If the records are available in the collection variable, we need to delete those records. To do this, we must add a Delete Record element and provide the variable in the Record or Record Collection field.

Exclude data using NOT IN in Salesforce Flow

Then, using the send email element to notify the lead owner that the record has been deleted successfully.

Use IN to simplify decision conditions in Flow

After that, save the flow and debug it before activating.

IN operator fetch related records in Salesforce
NOT IN condition for data cleanup flows

Proof of Concept:

Below you can see I have a record which is older than one year, and also it doesn’t have ‘Outbound Calls’ and ‘Partner Referral’ lead sources.

Use IN with Get Records in Salesforce Flow

After executing the flow, you can see that the lead owner received an email stating that the record has been deleted.

Using IN and NOT IN to control flow logic

Let’s check whether the record has been deleted. To do this, navigate to the lead object and search for the lead mentioned in the email.

You can see that before executing the query, the record was present, but now, as you search for it, the record is no longer visible, which means the record was deleted successfully.

Salesforce Flow with In and Not In Operators

In this way, we can utilize the “Not In” operator in Salesforce flow with picklist values to verify if a value is not included in a list of values. (collection variable).

Conclusion

I hope you have got an idea of how to use “In” and “Not In” operators in Salesforce Flow. In this, we have learned where it can be used and how to use it, with real-time examples and step-by-step explanations.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.