+ Start a Discussion
CloudGeekCloudGeek 

Error: 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.

Hi,

We have an after update trigger which pulls some tasks based on one field on Task record and update a field on opportunity.

The following loop:

for(Task t :[SELECT id,subject,WhatId,createdDate,Distributor_To_See__c FROM Task WHERE WhatId IN :opportunityIds AND Distributor_To_See__c = 'DSC' LIMIT 49999])
    {
        If(oppIdToTasks.ContainsKey(t.WhatId))
        {
                oppIdToTasks.get(t.WhatId).add(t);
        }
        else
         {
                List<Task> tempList = new List<Task>();
                tempList.add(t);
                oppIdToTasks.put(t.WhatId,tempList);          
        }
   
    }

that Query line in bold above - is causing this error.

Update error code CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: AutoShareTasks: execution of AfterUpdate

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.AutoShareTasks: line 23, column 1



Any one suggest what needs to be done for this ?

Thank you.

Best Answer chosen by CloudGeek
ShashankShashank (Salesforce Developers) 
Since the query is already a a FOR loop (well optimized), there are 2 options I can suggest:

1.) Split the query into parts as explained in this example: https://developer.salesforce.com/forums/ForumsMain?id=906F00000009Da4IAE
2.) Raise a case with salesforce to create a custom index on the filtering fields  (whatId or Distributor_To_See__c or both). Before that, see if you can mark the Distributor_To_See__c field as an external Id field so that a custom index is automatically created.