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
Deepika G PDeepika G P 

non-selective query against large object type ERROR

Hi All,

I have code written on a project schedule object. When i run this code it is throwing "non-selective query against large object type(more than 200000 rows)...." error.
When I debug it the query it is fetching one record only.Could any one help me on this please

 if(trigger.IsAfter){
    if(trigger.isInsert || trigger.isUpdate){
        list<Task>Tasks=new list<Task>();                
        for(Project_Schedule__c PSOne:trigger.new){
           if(PSOne.Assigned_To_User__c!=null){
                user u=[select id,name from user where id=:PSOne.Assigned_To_User__c limit 1];                      
                 project__c p=[select id,name,Due_Date__c,Priority__c from project__c where id=:PSOne.Project__c limit 1];
                string s=(string)PSOne.Name;
                list<task>TskList= new List<Task>();                            
                list<task> etask =[select id,Project_Schedule_Name__c FROM Task WHERE Project_Schedule_Name__c =: PSOne.id];-----IT IS THROWING ERROR ON THIS LINE.
                system.debug('88888'+etask);
                system.debug('77777'+etask.size());
                 if(PSOne.phase__c!=null && psone.Task_Completed__c ==false && etask.size()==0 ){
                 {                      
                        Task Tsk=new Task(WhatId=P.id, OwnerId=u.id, Status='Not Started', ActivityDate=PSOne.actual_date__c, Priority='Normal',                          Description=PSOne.comments__c,Subject=PSOne.Phase__c,project_schedule_Name__c=PSOne.Name);
                        
                            Tasks.add(Tsk);                          
                      }
                      
                      if(trigger.isUpdate)
                      {                                          
                            for(Project_Schedule__c  oldProj: trigger.old){
                                if((oldProj.Actual_Date__c != PSOne.Actual_Date__c) && etask.Size()>0){
                                    for(Task t : etask){
                                    t.ActivityDate = PSONE.Actual_Date__c;
                                    tasks.add(t);
                    }
                }

 
Best Answer chosen by Deepika G P
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue. I hope that will help you
1) https://help.salesforce.com/apex/HTViewSolution?urlname=How-to-make-my-SOQL-query-selective&language=en_US
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm
3) http://help.salesforce.com/HTViewSolution?id=000002493




Description
If you're running a trigger on objects that have more than 100,00 records, it's possible that you'll receive the error, "System.QueryException: Non-selective query against large object type." We'll go over the a few possible fixes. 

Resolution
Options to resolve error
1. You may find that the query in question needs to be more selective in the WHERE clause. According to the Salesforce standards & best practices - the where clause needs to subset 10% or less of the data.
2. A custom index on the field.
3. A possible quick fix may be to make the field in question an external ID. Since external IDs are indexed automatically, this will create the index and may solve the problem.
A. Go to Setup.
B. Under "Build", click Customize then click the object name.
C. Click Fields.
D. Then, edit field name and check the box External ID.

Let us know if this will help you

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue. I hope that will help you
1) https://help.salesforce.com/apex/HTViewSolution?urlname=How-to-make-my-SOQL-query-selective&language=en_US
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm
3) http://help.salesforce.com/HTViewSolution?id=000002493




Description
If you're running a trigger on objects that have more than 100,00 records, it's possible that you'll receive the error, "System.QueryException: Non-selective query against large object type." We'll go over the a few possible fixes. 

Resolution
Options to resolve error
1. You may find that the query in question needs to be more selective in the WHERE clause. According to the Salesforce standards & best practices - the where clause needs to subset 10% or less of the data.
2. A custom index on the field.
3. A possible quick fix may be to make the field in question an external ID. Since external IDs are indexed automatically, this will create the index and may solve the problem.
A. Go to Setup.
B. Under "Build", click Customize then click the object name.
C. Click Fields.
D. Then, edit field name and check the box External ID.

Let us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
Deepika G PDeepika G P
Amit,

There are no task for that project schedule object. still it is throwing this error.
 list<task> etask =[select id,Project_Schedule_Name__c FROM Task WHERE Project_Schedule_Name__c =: PSOne.id];
 if(PSOne.phase__c!=null && psone.Task_Completed__c ==false && etask.size()==0 ){
                 {                      
                        Task Tsk=new Task(WhatId=P.id, OwnerId=u.id, Status='Not Started', ActivityDate=PSOne.actual_date__c, Priority='Normal',                          Description=PSOne.comments__c,Subject=PSOne.Phase__c,project_schedule_Name__c=PSOne.Name);
                        
                            Tasks.add(Tsk);                          
                      }
For the above code the task is created only when project schedule record is created ,it there is only one task created for one project schedule record.


 
Amit Chaudhary 8Amit Chaudhary 8
Issue is not coming because number of record return by query. Issue is coming because you are trying to query from that object which have more then 1,00,000 records.

If you're running a trigger on objects that have more than 100,00 records, it's possible that you'll receive the error, "System.QueryException: Non-selective query against large object type."
 
Deepika G PDeepika G P

Thanks Amit I googled this error. Found the below option too.

A. Go to Setup.
B. Under "Build", click Customize then click the object name.
C. Click Fields.
D. Then, edit field name and check the box External ID.

BUT I thought It is because of the query since it was throwing the line number in the error  thanks I will check with this options and see if it works.
Amit Chaudhary 8Amit Chaudhary 8
Yes same solution i posted in my first post. Also shared some more helpfull link. I hope that will help you
Deepika G PDeepika G P
Thanks Amit for your help.

 
Deepika PatelDeepika Patel
Hi ALL,

I have a code where lastmodifiedbyid is not working correctly all the time. its an after trigger,could you please help me on this.

 system.debug('Error in the Log'+ PSOne.Name);
                    Task ts=[select id,Priority,ActivityDate,createdById,LastModifiedByid from Task where Project_Schedule_Name__c =: PSOne.Name limit 1];
                     system.debug('444444'+ts.LastModifiedByid);  
                    user u2=[select id,name from user where id=:ts.LastModifiedByid limit 1];    
                    system.debug('66666'+u2.name);