I am getting 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 a 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) Exception while trying to convert a Lead.
The exception occurred in the last line of the below code,
List<Opportunity> oppsToProcess = new List<Opportunity>();
Set<String> prospectIds = new Set<String>();
for (Opportunity opp: newOpps) {
if (opp.ConId__c != null) {
opp.Prospect_Id__c = opp.ConId__c;
opp.ConId__c = null;
}
if (!opp.IsClosed && opp.Prospect_Id__c != null && opp.SAL_Date__c != null) {
oppsToProcess.add(opp);
prospectIds.add(opp.Prospect_Id__c);
}
}
if (oppsToProcess.isEmpty()) { return; }
Set<Id> contactIds = new Set<Id>();
String soql = 'SELECT ' + String.join(leadFields, ', ') + ' FROM Lead WHERE ProspectId__c IN :prospectIds';
Map<String, Lead> mapLead = new Map<String, Lead>();
for (Lead lead: (List<Lead>)Database.query(soql)) {
}
- My org has 150000 Lead records only, In the worst case, it's less than 200000 record limit.
- I have another concern about the null check (ConId__c, Prospect_Id__c are Text fields), But I have checked that the null and the empty ('') acts in the same way.
Did I miss anything over there in my code?