Skip to main content

Organize Data in a Collection Variable

Learning Objectives

After completing this unit, you’ll be able to:

  • Filter the contents of a collection variable.
  • Sort the contents of a collection variable.
  • Limit the number of values in a collection variable.
Note

This badge is one stop along the way to Flow Builder proficiency. From start to finish, the Build Flows with Flow Builder trail guides you through learning all about Flow Builder. Follow this recommended sequence of badges to build strong process automation skills and become a Flow Builder expert.

When working with collection variables, you may need to change the contents of the collection before putting it to use. In Flow Builder, you can use elements to remove unwanted values from the collection, change the order of the collection’s values, or limit the collection to a specific number of values. These functions can be accomplished with the Collection Filter and Collection Sort elements. Let’s take a look at these elements and why they’re useful.

Filter a Collection’s Contents

The Collection Filter element does exactly what it says on the label: it applies a filter to a collection, removing anything that doesn’t meet the filter criteria. Well actually, that’s not entirely true. The collection you enter remains untouched, and the Collection Filter element creates a duplicate collection that doesn’t contain the values that don’t meet the criteria. This behavior can be useful, because the original unfiltered collection is still usable if you need it.

Flowchart showing that Collection Filter elements create a modified copy of the original record collection.

For example, let’s say you have a text collection that contains email addresses for a new service announcement email, but you want to avoid sending this message to email addresses that include your competitor’s domain. The flow now has two text collections: one with all of the originally gathered email addresses, and one without the competitor’s email addresses.

Sort and Truncate a Collection’s Contents

With the Collection Sort element, you can change the order of a collection based on criteria that you set. For example, you can reorder a collection that contains Opportunity records by Amount, in descending order.

However, the Collection Sort element has one more important function: It can limit the number of values in a collection, removing all values after that limit. After you sort a collection in descending order by Amount, you might choose to keep only the top 10 opportunities. All ‌other opportunities are removed from the collection.

It’s important to note that, unlike the Collection Filter element, the Collection Sort element does not create a new collection. Instead, it modifies the collection that you select for sorting.

Flowchart showing that Collection Sort elements modify the original record collection.

You may be thinking, “Wait a second, filtering, sorting, and limiting sound familiar. Can’t I do those things in a Get Records element?” Yes, you can, and it is best practice to use the Get Records element’s functionality when possible. It’s more efficient and runs faster than adding more elements. Use the Collection Filter and Collection Sort elements when you can’t determine the data’s criteria at the time of the Get Records, such as when the criteria is determined by a Decision element or screen inputs.

Filter and Sort in a Flow

Let’s look at a scenario where you build a flow with Collection Filter and Collection Sort elements. The Product Marketing team uses an agent to manage their campaigns and has requested that it has the ability to quickly add campaign members to a product-based campaign. High-investing customers have a high rate of rebuying, so Product Marketing has asked to have the agent identify the top 10 won opportunities whose main product matches the campaign’s Product Campaign Type, and add those opportunities’ contacts as campaign members.

Create a Flow and Get the Required Records

Create an Autolaunched flow (the No Trigger kind). Then add a variable to receive the campaign ID from the prompt template, and some Get Records elements to retrieve the data that the flow needs.

  1. Create an Autolaunched flow (No Trigger).
  2. Create a variable to receive the campaign’s ID.
    • For API Name, enter recordId.
    • For Data Type, select Text.
    • For Availability Outside the Flow, select Available for input.
      The Available for Input setting must be enabled on any variable that should receive data from an agent.
  3. On the flow canvas, add a Get Records element.
    • For Label, enter Get Campaign Details.
      The API Name should auto-populate as Get_Campaign_Details.
    • For Object, select Campaign.
    • For Condition Requirements, select All Conditions Are Met (AND).
    • For Field, select Campaign ID.
    • For Operator, select Equals.
    • For Value, select recordId.
    • Leave the remaining fields as default.
  4. After the Get Campaign Details element, add another Get Records element.
    • For Label, enter Get Opps.
      The API Name should auto-populate as Get_Opps.
    • For Object, select Opportunity.
    • For Condition Requirements, select All Conditions Are Met (AND).
    • For Field, select Stage.
    • For Operator, select Does Not Equal.
    • For Value, select Closed Lost.
    • For How Many Records to Store, select All records.
    • Leave the remaining fields as default.
  5. Save the flow.
    • For Flow Label, enter Create Top 10 Campaign Members.
      The Flow API Name should auto-populate as Create_Top_10_Campaign_Members.

Check the Product Campaign Type with a Decision

The flow must behave differently based on the campaign’s Product Campaign Type, so let’s create a Decision element with different paths for the most popular product categories.

  1. After the Get Opps element, add a Decision element.
    • For Label, enter What Product Campaign Type?
      The API Name should auto-populate as What_Product_Campaign_Type.
  2. In the Outcome Details:
    • For Label, enter Generators.
    • For Outcome API Name, enter Generators.
    • For Condition Requirements to Execute Outcome, select All Conditions Are Met (AND).
    • For Resource, select Campaign from Get Campaign Details, then select Product Campaign Type.
    • For Operator, select Equals.
    • For Value, select Generators.
  3. Create a new outcome with the following details:
    • For Label, enter Bots.
    • For Outcome API Name, enter Bots.
    • For Condition Requirements to Execute Outcome, select All Conditions Are Met (AND).
    • For Resource, select Campaign from Get Campaign Details, then select Product Campaign Type.
    • For Operator, select Equals.
    • For Value, select Bots.
  4. In the Outcome Order, select Default Outcome.
    • For Label, enter Other.
  5. Save the flow.

Filter and Sort the Collection

Now that we have separate paths for each product, create a Collection Filter element that generates a collection that contains only opportunities where the main product is a generator. Generator opportunities have a value of GC in the Main Product Category field.

  1. On the Generators path, add a Collection Filter element.
    • For Label, enter Filter Generators.
      The API Name should auto-populate as Filter_Generators.
    • For Collection, select Opportunities from Get Opps.
    • For Condition Requirements, select All Conditions Are Met (AND).
    • In the first filter condition:
      • For Field, select Main Product Category.
      • For Operator, select Equals.
      • For Value, select GC.

Then, add a Collection Sort element that pares down the new Generators opps collection to just the top 10 opps with the highest Amounts.

  1. On the Generators path, after the Collection Filter element, add a Collection Sort element.
    • For Label, enter Top 10 Generator Opps.
      The API Name should auto-populate as Top_10_Generator_Opps.
    • For Collection Variable, select Opportunities from Filter Generators.
      Remember, Collection Filter elements create a new collection variable. So to build upon the filtering performed in the Filter Generators element, you select its collection variable.
    • For Sort By, select Amount.
    • For Sort Order, select Descending.
    • For How Many Items to Keep After Sorting, select Set the maximum number of items.
    • In the field that appears after that selection, enter 10.
  2. Save the flow.

Transform and Create Campaign Members

You now have exactly the data you want: a collection of the top 10 opportunities that aren't Closed Lost and that have a Main Product Category of GC, sorted by Amount. It’s time to take the contacts from those opportunities and transform them into campaign members. Lastly, use a Create Records element to create the campaign member records from the transformed campaign member collection.

  1. On the Generators path, after the Collection Sort element, add a Transform element.
    • For Label, enter Transform Generator Contacts to CMs.
      The API Name should auto-populate as Transform_Generator_Contacts_to_CMs.
    • In the Source Data box, click Add flow resource for source data.
      • For Resource, select the Opportunities from Filter_Generators record collection variable.
        Remember, the Collection Sort element does not create a new collection, so you still reference the collection generated by the Collection Filter element.
    • In the Target Data box, click Add flow resource for target data.
      • For Data Type, select Record.
      • Select Allow multiple values (collection).
      • For Object, select Campaign Member.
      • Click Create.
  2. Map the Source Data’s ContactId to the Target Data’s ContactId.
  3. Assign a formula to the CampaignId. The formula should set the CampaignId to the ID of the initial campaign.
    • In the Target Data column, click CampaignId.
    • Click Add or edit formula, then click Formula.
    • In the Enter value or search resources… field, select the recordId variable.
      The Formula field should now contain {!recordId}.
    • Click Check Syntax to verify the formula.
  4. Click Close to close the Transform panel.
  5. On the Generators path, after the Transform element, add a Create Records element.
    • For Label, enter Create Generator CMs.
      The API Name should auto-populate as Create_Generator_CMs.
    • For How to set record field values, select From a Record Variable.
    • For How Many Records to Create, select Multiple.
    • For Record Collection, select Campaign Members from Transform Generator Contacts to CMs.
  6. Save the flow.
    The completed flow corresponding to the preceding steps.

Way to flow! Finish this flow in the hands-on challenge by creating the elements in the Bots path.

Before you leave, remember the most important rule of Transforms: use a Transform instead of a Loop whenever possible. Watch for future badges about loops and additional Transform features, right here on Trailhead.

Resources

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback