+ Start a Discussion

Is there a way to write a test class to to test 'Non-selective query against large object type'

Hello - I need to test the below query. Is there a way to test for this specific exception in a test class?  'Non-selective query against large object type' 

existingLead = [SELECT Id, street, LastName, PostalCode,                                                                 LastNameStreetAndZipCode__c, Core_Number_Text__c,                                     Status, Home_Phone__c, CreatedDate, Core_Number__c,                                   Policy_Core_Number__c, Lead_Type__c
                                FROM LEAD
                                WHERE ((LastNameStreetAndZipCode__c IN                                                                            :lastNameStreetZipSet  
                                AND Core_Number__c IN :coreNumberTextSet)
                                OR (LastNameStreetAndZipCode__c IN                                                                             :lastNameStreetZipSet)
                                OR (LastNameStreetAndZipCode__c IN :setOfPoBoxes))
                                AND Household__c != null
                                AND Duplicate__c != True
                                AND IsDeleted = False
                                ORDER BY CreatedDate ASC
                                LIMIT 20000];
SwethaSwetha (Salesforce Developers) 
Based on https://help.salesforce.com/s/articleView?id=000323572&type=1, If you're running a SOQL query on an object with more than 200,000 records while running triggers, it's possible that you'll receive the "System.QueryException: Non-selective query against large object type." error.

I have not tried myself but you might want to create more than 200,000 records to test this behaviour.Also recommend reviewing https://salesforce.stackexchange.com/questions/194897/system-queryexception-non-selective-query-against-large-object-type-more-than

If this information helps, please mark the answer as best. Thank you