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
R R MR R M 

Query Error: System.QueryException: Non-selective query against large object type

HI Folks, 
Getting Debuglog Error, 

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)

Below is my Trigger:

Trigger CaseCustomer on Case (before insert,after update) {
    Try{
    Set<String> stemail = new Set<String>();
    List<Customer__c> custlst = new List<Customer__c>();
    Map<String,Customer__c> mpemailtocustomer = new Map<String,Customer__c>();
    List<Case> updcaselst = new List<Case>();
    Map<String,Entitlement> mpentitle = new Map<String,Entitlement>();
    Set<Id> casid = new Set<Id>();
    List<CaseMilestone> cmsToUpdate = new List<CaseMilestone>();
    
    if(Trigger.isAfter)
    {
        for(Case icas:Trigger.New)
        {
            if((trigger.newMap.get(icas.id).Status!= trigger.oldMap.get(icas.id).Status) && icas.Status == 'Closed')
            {
                casid.add(icas.id);
                System.debug(casid);
            }
        }
        if(casid.size()>0)
        {
            cmsToUpdate = [select Id,completionDate from CaseMilestone where caseId IN :casid];
            System.debug(cmsToUpdate);
        }
        if(cmsToUpdate.size()>0)
        {
            for(CaseMilestone icasn:cmsToUpdate)
            {
                icasn.completionDate = System.now();
            }
            update cmsToUpdate;
        }
        
    }
    
   else
   {
    for(Case icas:Trigger.New)
    {
        if(icas.SuppliedEmail != null)
        {
            stemail.add(icas.SuppliedEmail);
            System.debug(stemail);
        }
    }
    if(stemail.size()>0)
    {
        custlst = [SELECT ID,Email__c FROM Customer__c WHERE Email__c IN:stemail];
        System.debug(custlst);
    }
    if(custlst.size()>0)
    {
        for(Customer__c icus:custlst)
        {
            mpemailtocustomer.put(icus.Email__c,icus);
            System.debug(mpemailtocustomer);
        }
    
    }
    List<Entitlement> entlst = [SELECT id,Name FROM Entitlement];
    if(entlst.size()>0)
    {
        for(Entitlement ient :entlst)
        {
            mpentitle.put(ient.Name,ient);
        }
    }
    
        for(Case icas : Trigger.New)
        {
            if(mpemailtocustomer.containskey(icas.SuppliedEmail))
            {
                icas.Customer__c = mpemailtocustomer.get(icas.SuppliedEmail).ID;           
            }
            if(icas.Origin == 'Email' ||  icas.Origin == 'Phone' || icas.Origin == 'Web')
            {
                icas.EntitlementId = mpentitle.get('Response').Id;
                System.debug(icas.EntitlementId);
            }
        }
    
    }
    }catch(exception e)
    {
        System.debug(e.getlinenumber()+e.getmessage());
    }
}

Please Do help,
Thanks In Advance.
Best Answer chosen by R R M
R R MR R M
I got solution. 

Just changed Email for Externally available.

All Answers

Gokula KrishnanGokula Krishnan
Hi RRM,

Try to put filter conditions in your SOQL query.

For Example:
Line 23: select Id,completionDate from CaseMilestone where caseId IN :casid and Status != null and Status = 'Close' //something like this based on your logic.

Check all your query lines 23, 49, 61.

I hope, in line 61, you're querying all values, thats why you're getting this error. Try to add filter and check it out.

Regards,
Gokula Krishnan

If it helps you, please mark is as best answer, so it will be helpful for other developers.
R R MR R M
Hi Gokula Krishnan, 

I have added fields in Query and Deployed to production but Same error am getting. Trigger Not Working in Production. 
Gokula KrishnanGokula Krishnan
Hi RRM,

In which SOQL query line, error is occuring..?

Regards,
Gokula Krishnan
R R MR R M
I got solution. 

Just changed Email for Externally available.
This was selected as the best answer