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
Ralf WittenbergerRalf Wittenberger 

Indexed Filter on Trigger

Hi,
i have a Trigger where I am searching for a value in a case to lookup on accounts to fill in the account id to the case.

The Trigger works well so far in the sandbox, but when i want to validate to get in in production deployed, it returns the following issue:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountOnCases: 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.AccountOnCases: line 7, column 1 


Here is the trigger:

trigger AccountOnCases on Case (before insert) {
List<String> Account = new List<String>(); 
for (Case a:Trigger.new)
{
Account.add(a.Customer_Number_Lookup_C__c);
}
List <Account> AccountList = [Select ID from Account where Customer_Number__c in :Account];
    if (AccountList.size() > 0) {
        for (Integer i = 0; i < Trigger.new.size(); i++)
            
        
if (Trigger.new[i].Customer_Number_C__c != null) 
{
Trigger.new[i].AccountID = AccountList[i].ID;
}
else
{
Trigger.new[i].AccountID = null;
}
}
}

Do you have an idea, how to solve this?

Thanks in advance

Ralf
Best Answer chosen by Ralf Wittenberger
Pankaj_GanwaniPankaj_Ganwani
Hi,

Such type of errors occurs when we have NULL value in filter criteria of SOQL. Please make sure you don't have NULL value in Account list.
Perform NULL check before adding the values to list:

for (Case a:Trigger.new)
{
if(a.Customer_Number_Lookup_C__c!=null)
     Account.add(a.Customer_Number_Lookup_C__c);
}

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

Such type of errors occurs when we have NULL value in filter criteria of SOQL. Please make sure you don't have NULL value in Account list.
Perform NULL check before adding the values to list:

for (Case a:Trigger.new)
{
if(a.Customer_Number_Lookup_C__c!=null)
     Account.add(a.Customer_Number_Lookup_C__c);
}
This was selected as the best answer
Ralf WittenbergerRalf Wittenberger
Thanks!!!