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
Abhishek RayAbhishek Ray 

Case Trigger - Causing problem for non-selective query in a trigger executes against an object

I am working on trigger for email to case which is working. But I have a new problem. The trigger is causing aproblem from when ticket is created through Salesforce(logging in salesforce and creatig a ticket). I am trying to create a ticket, this is the error I am getting.

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger CaseContactOwnerName caused an unexpected exception, contact your administrator: CaseContactOwnerName: execution of BeforeInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 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): Trigger.CaseContactOwnerName: line 13, column 1

Here is trigger:

trigger CaseContactOwnerName on Case(before insert)

{

List<Case> newCase = new List<Case>();

List<Contact> con = new List<Contact>();

List<String> emailList = new List<String>();


for(Case c: trigger.new)

{

system.debug('*********' + Trigger.new);

 

//create a new email id for the case(s) being created emailList.add(c.SuppliedEmail); }

con= [select id from Contact where email=:emailList];
}

Please help me in making query selective in the above code because I have no idea what have to be done?
 

Thanks, Abhishek Ray

Best Answer chosen by Admin (Salesforce Developers) 
craigmhcraigmh

You could always try to add useless filters to the query.

 

con = [Selet Id From Contact Where email <> null And email In :emailList Limit 10000];

 

I think that's how I got by this error when I had it...

All Answers

craigmhcraigmh

Since you loop through the Cases and add the SuppliedEmail field to emailList, you'll have a full list after the loop. You then query once AFTER the for loop to get all matching contacts.

 

for(Case c: trigger.new) {
	emailList.add(c.SuppliedEmail);
}

con = [Selet Id From Contact Where email In :emailList Limit 10000];

 

Abhishek RayAbhishek Ray

Sorry your solution didn't work. Same error.

craigmhcraigmh

You could always try to add useless filters to the query.

 

con = [Selet Id From Contact Where email <> null And email In :emailList Limit 10000];

 

I think that's how I got by this error when I had it...

This was selected as the best answer
Abhishek RayAbhishek Ray

Thanks you so much it worked.