Using the IN operator with sorting in a Flow Get Records element can produce an unexpected SOQL syntax error when the collection variable used in the IN clause is null. Learn why this happens and simple safeguards to avoid it.
Summary
When you configure a Get Records element in Salesforce Flow with an Id filter using the IN operator and also enable sorting (Order By), the underlying generated SOQL can fail with a syntax error if the collection variable bound to the IN clause is null or empty. This is an engine-side issue where Flow constructs an invalid SOQL string when the collection has no values.
Why this matters
Flows are used to automate business logic without code, and Get Records is a common element. A silent SOQL syntax error can break a Flow at runtime and be hard to diagnose — especially if the collection variable sometimes resolves to null (for example, if previous logic didn’t populate it). Preventing this keeps automations reliable and reduces troubleshooting time.
Best practice and resolution
Recommended approach:
- Always check that a collection/text collection variable is not null or empty before using it in a Get Records IN filter.
- Use a Decision element to branch: if the collection is empty, skip the Get Records (or use alternative logic); otherwise, run the Get Records with IN and sorting.
- Where possible, initialize collections with an empty list value so they are not null, or populate them defensively in prior steps.
Example Flow pattern
- Decision: Is VariableIdsCollection null or empty?
- If No: Get Records (Filter: Id IN VariableIdsCollection; Sort by CreatedDate DESC)
- If Yes: Skip Get Records or handle alternative path
Additional tips
- Testing: Add debug logs and explicit Flow fault handling to capture the SOQL syntax error earlier in testing environments.
- Optimization: A null-check avoids running a redundant Get Records and therefore avoids a SOQL query — improving performance.
- Reference: A Trailblazer Community thread documents similar behavior and troubleshooting steps.
Conclusion
For Salesforce admins and developers building Flows, the lesson is simple: validate collection variables before using them in filters that convert to SOQL IN clauses, especially when combined with sorting. This small defensive pattern prevents runtime SOQL syntax errors, reduces failures in production automations, and simplifies debugging.
Why this matters for admins, developers, and business users: reliable Flows keep business processes running, reduce customer impact, and save time during deployments and maintenance. Adding a Decision-based null check is a low-effort, high-impact change that improves Flow resilience.








Leave a Reply