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
SabrentSabrent 

Non-selective query against large object type

Can someone please point me in the right direction?hanks.

 

Error Message: 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)
Error System Trace: Class.HelperClass.newEval: line 982, column 1 Trigger.triggername: line 36, column 1

 

trigger triggername on Object__c (after insert, after update) {

 if(trigger.isInsert){
        List<Object__c> evaluations = [Select Id From Object__c];
    
        Map<Id, List<Id>> junk = new Map<Id, List<Id>>();
        for (Object__c obj : evaluations) {
        
            junk.put(obj.Id, new List<Id>());   
        }

        for (Employee_Object__c empobs : Trigger.new) {
            if (junk.containsKey(empobs.Object__c)) {
                junk.get(empobs.Object__c).add(empobs.Id);            
            }       
        }
    
    
        
        HelperClass.newEval(trigger.new); // line36        
    }
 ......
 
}

 

public without sharing class HelperClass {
  public static void newEval(list<Object__c> newEV){
        try {
            //Get List of Employee IDs, OS
            set<id> empIDs = new set<id>();
            set<id> cnIds = new set<id>();
            for(Object__c ev: newEV){
                empIDs.add(ev.Employee__c);
                cnIDs.add(ev.Group_Leader_Id__c);
            }
            list<Employee__c> emps = [select id,Manager_Id__c,Group_Leader_Id__c,SC_ID__c from Employee__c where id in:empIDs];
            map<id,Employee__c> mp_emps = new map<id,Employee__c>(emps);
            
            //Get User IDs from Contact         
            for(Employee__c e: emps){
                cnIds.add(e.Group_Leader_Id__c);
                cnIds.add(e.SC_ID__c);
            }
            
            list<user> usr = [select id, contactID from user where contactID in: cnIDs]; // line982            map<id,id> usrIDs = new map<id,id>(); //first ID is the Contact
            for(user u:usr){
                usrIDs.put(u.contactID,u.id);
            }
   
   
   .......

}

 

 


 

Saikishore Reddy AengareddySaikishore Reddy Aengareddy
Try this.. Sometimes nulls in set might cause this error..
 
 
public without sharing class HelperClass {
  public static void newEval(list<Object__c> newEV){
        try {
            //Get List of Employee IDs, OS
            set<id> empIDs = new set<id>();
            set<id> cnIds = new set<id>();
            for(Object__c ev: newEV){
                empIDs.add(ev.Employee__c);
                if(ev.Group_Leader_Id__c<>null)
                cnIDs.add(ev.Group_Leader_Id__c);
            }
            list<Employee__c> emps = [select id,Manager_Id__c,Group_Leader_Id__c,SC_ID__c from Employee__c where id in:empIDs];
            map<id,Employee__c> mp_emps = new map<id,Employee__c>(emps);
            
            //Get User IDs from Contact         
            for(Employee__c e: emps){
                if(e.Group_Leader_Id__c<>null)
                cnIds.add(e.Group_Leader_Id__c);
                if(e.SC_ID__c<>null)
                cnIds.add(e.SC_ID__c);
            }
            
            list<user> usr = [select id, contactID from user where contactID in: cnIDs]; // line982            map<id,id> usrIDs = new map<id,id>(); //first ID is the Contact
            for(user u:usr){
                usrIDs.put(u.contactID,u.id);
            }
   
   
   .......
 
}
Grazitti InteractiveGrazitti Interactive

Hi,

 

While working with very large SOQL series , query in for loop or use Apex Batch Class.

 

public without sharing class HelperClass {
  public static void newEval(list<Object__c> newEV){
        try {
            //Get List of Employee IDs, OS
            set<id> empIDs = new set<id>();
            set<id> cnIds = new set<id>();
            for(Object__c ev: newEV){
                empIDs.add(ev.Employee__c);
                if(ev.Group_Leader_Id__c<>null)
                cnIDs.add(ev.Group_Leader_Id__c);
            }
            list<Employee__c> emps = [select id,Manager_Id__c,Group_Leader_Id__c,SC_ID__c from Employee__c where id in:empIDs];
            map<id,Employee__c> mp_emps = new map<id,Employee__c>(emps);
            
            //Get User IDs from Contact         
            for(Employee__c e: emps){
                if(e.Group_Leader_Id__c<>null)
                cnIds.add(e.Group_Leader_Id__c);
                if(e.SC_ID__c<>null)
                cnIds.add(e.SC_ID__c);
            }
            
           // list<user> usr = [select id, contactID from user where contactID in: cnIDs]; // line982          
            map<id,id> usrIDs = new map<id,id>(); //first ID is the Contact
         
           for(user u:[select id, contactID from user where contactID in: cnIDs]){
                      usrIDs.put(u.contactID,u.id);//ContactId is lookup and indexable
            }
                     /******OR****/
          
 
            for(user u:[select id, contactID from user where contactID in: cnIDs and contactID != null]){

                      usrIDs.put(u.contactID,u.id);//ContactId is lookup and indexable
            }
             /*********OR modify query as*******
             //[select id, contactID from user where contactID in: cnIDs and contactID != null and isActive =: true and isPortalEnabled =: true];
                                
           ......
 
   }
 
  If this post helps you please throw a kudos by clicking on right side star.
 
Thanks



SabrentSabrent

Thanks very much SaiKishore and Grazitti. Your suggestions are much appreciated.

I will test and let you know how I go..