function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gary WGary W 

Flow error when getting one record: System.QueryException: Non-selective query against large object type (more than 200000 rows).

My flow is pretty simple, it simply returns the account record found for a given account number, along with the record type name.

The whole flow looks like this:
User-added image

The Get Account element is below. This is the element that is erroring.
User-added image

When I debug the flow, the debug output looks like this for the Get_Account step:
 
Fast Lookup: Get_Account
Find all Account records where
AccountNumber Equals {!VarAccountNumber} (A00012345)
Sort records by: CreatedDate (Ascending)
Store the values of these currently referenced fields in Get_Account: RecordTypeId, Id
Because Get_Account is passed to an action, subflow, or Lightning component, store the values of all Account fields that the running user has access to.

Result
Failed to find records.

Error Occurred:
This error occurred when the flow tried to look up records: An unexpected error occurred. Please include this ErrorId if you contact support: 908556789-5354 (-548403183). You can look up ExceptionCode values in the SOAP API Developer Guide.

When I look in the developer console logs, I can see the following error occuring:
System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)

I found this knowledge article (https://help.salesforce.com/articleView?id=000323572&type=1&mode=1) which gives some further detail on the error. It suggests that queries should return less 10% or less of the data. My query would only return 1 record. I currently have a total of ~364k account records. This shouldn't be a problem.

I've tried writing a SOQL query that would be equivalent of what's being queried in the flow. It looks like this:
SELECT [all fields] FROM Account WHERE AccountNumber = 'A00012345'
Replacing [all fields] with all my account object fields, and running the query, it returns 1 row without any errors.

According to all the salesforce documentation I could find, this shouldn't be a problem, yet for some reason the flow encounters this issue. 

I've tried adding further conditions to the Get_Account element, to try and make the query more selective:
User-added imageThis did not resolve the issue and the same error occurs.

The other suggestion in the Salesforce documentation is to index the field. Since Account Number is a standard field, I can't make it an external ID, so I'll need to contact support for this.

Can anyone explain why this error is affecting me, when all the documentation, and test queries I run suggest that it shouldn't be occurring?
Best Answer chosen by Gary W
AbhishekAbhishek (Salesforce Developers) 
I have seen this kind of issue, Based on that telling you after perform indexing your will issue will be resolved.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.

All Answers

AbhishekAbhishek (Salesforce Developers) 
common.apex.runtime.impl.ExecutionException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)


Based on the error ID -548403183 I have checked in our server logs and found the below information.

To Overcome the issue you have to perform indexing or add more filters to your SOQL query.

For indexing, you to need contact salesforce Support.

Salesforce will help you with these scenarios even you don't have premier support.


For further reference, you can check the below article,

https://help.salesforce.com/articleView?id=000002493&r=https%3A%2F%2Fwww.google.com%2F&type=1


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Regards,
Salesforce Support.
Gary WGary W
Thanks Abhishek,

You say to add more filters to the query, however my query is already only returning 1 record. It cannot get any more selective than that.

I will contact support about custom indexing though.
AbhishekAbhishek (Salesforce Developers) 
I have seen this kind of issue, Based on that telling you after perform indexing your will issue will be resolved.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
This was selected as the best answer
Marcos LasoMarcos Laso
One trick to index your fields without contacting support is marking them a External Ids (https://help.salesforce.com/articleView?id=000320036&type=1&mode=1) (https://help.salesforce.com/articleView?id=000320036&type=1&mode=1)
Gary WGary W
Thanks Marcos,

In my case, the field (Account number) was a standard field, therefore could not be modified.

I ended up working with support. They indexed the field, which solved the problem straight away.