Avoid Salesforce Flow Errors: Limit Get Records to Stay Under 50001 Rows

Advertisements

Last Updated on February 5, 2025 by Rakesh Gupta

Big Idea or Enduring Question:

  • How can I limit the number of records returned by the Get Records element in Salesforce Flow?

Objectives:

After reading this blog, you’ll be able to:

  1. Learn how to limit the number of records returned by the Get Records element.
  2. Avoid Flow errors by applying filters for efficient data retrieval.
  3. Optimize Flows to stay within Salesforce governor limits.
  4. And much more!

Business Use case

Olivia Bennett, a Junior Developer at Gurukul on Cloud (GoC), was exploring the Salesforce Spring ’25 release notes when she came across a new feature that allows developers to control the number of records retrieved with the Get Records element.

Her mentor, Harper Davis, introduced a simple use case to demonstrate how to use this new feature effectively:

Retrieve the 2000 most recently created leads where the Email field is populated.

What Is the Get Records Element?

Salesforce stores data in objects. Some objects are available in Flow as part of global variables. For example, User, UserRole, and Profile are system-provided variables that can be referenced throughout the Flow without requiring a query.

For other objects, such as Account or Lead, the records must be fetched from their respective tables. This is where the Get Records element comes into play, allowing you to retrieve data from the respective object. For instance, you can use the Get Records element to fetch all active Campaigns.

The Get Records element is more or less equivalent to List Views, as it enables you to filter and fetch specific records based on defined criteria.

Why Is Setting the Number of Records to Store Important?

One of the most common governor limits users encounter when using the Get Records element is the Too many query rows: 50001 error. This error occurs when the query retrieves more than 50,000 records, exceeding Salesforce’s governor limits, and results in a runtime error. This issue is especially prevalent when working with large datasets and attempting to fetch all records without proper filtering.

When dealing with large data volumes (LDV), using the Get Records element without constraints can lead to performance issues or exceed Salesforce governor limits. To avoid such errors and optimize Flow performance, now after spring’25 release you can set an upper limit on the number of records retrieved. By using the All records, up to a specified limit option, you can control the maximum number of records fetched, ensuring the Flow runs efficiently and stays within the limits.

Automation Champion Approach (I-do):

To solve the above business use case, feel free to create any type of Flow. I will skip the Flow creation steps and directly jump to the Get Records element to retrieve the 2000 most recently created leads where the Email field is populate.

  1. On Flow Designer, click on the +icon and select the Get Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Lead object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: Email
      2. Operator: Is Null
      3. Value: {!$GlobalConstant.False}
  6. Sort Lead Records
    1. Sort Order: Descending 
    2. Sort By: CreatedDate
  7. How Many Records to Store:
    1. Select All records, up to a specified limit
      1. Maximum Number of Records to Store: 2000
  8. How to Store Record Data:
    1. Choose the option to Automatically store all fields
  9. Click the X at the top to save your changes.

Proof of Concept

I used the debug log to verify whether the above flow is working correctly. The flow is returning 9 records, which matches my expectations. I am running this in a developer org.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Go back

Your message has been sent

Warning
Warning
Warning
Warning
Warning

Warning.

4 thoughts on “Avoid Salesforce Flow Errors: Limit Get Records to Stay Under 50001 Rows

  1. what about the other data which is not fetched by the first query. For example you have overall 10k records and want to get them in 5 queries with each 2k records. Is this possible?

    1. I don’t need all the data, which is why I set the limit to 2K. Currently, there is no clean way to fetch the data the way you want without inserting some workaround in between.

  2. Thank you so much for your clear and concise explanation of how to limit the number of records returned by the Get Records element in Salesforce Flow.

    This will definitely help me optimize flows and improve performance within our organization. I appreciate you taking the time to share this valuable information.

    1. Thanks for reading 😊

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version
%%footer%%