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
Chris PulliamChris Pulliam 

Lead Trigger: System.QueryException: Non-selective query against large object type

I'm very new to Apex. I tried writing a trigger to help solve an orphan lead problem in our instance, and relate leads to accounts. It looks at all leads in our instance with the same domain, and either attaches it to an account if one exists or creates an account, and attaches the lead.

I'm almost there, but when I brought it into production, and try creating or updating a lead, and I get this nasty error message. I pasted the error and code below. Does anyone know how to get through this?

Error:attachLeadToAccount1: execution of BeforeUpdate 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.attachLeadToAccount1: line 11, column 1 No record updated

Trigger:
trigger attachLeadToAccount1 on Lead (before Insert, before update) {

  // Get Lead Ids from all of the triggered Tasks
  set<string> leadIds = new set<string>();
  for(Lead myTask :trigger.new) {
    leadIds.add(myTask.domain__c);
  }

  // Get Leads and build a map of <LeadId,Lead_Accounts__c>
  map<string,id> mLeadAccountIds = new map<string,id>();
  list<Lead> leads = [SELECT domain__c, Lead_Accounts__c FROM Lead WHERE domain__c IN :leadIds];
  for(Lead l :leads) {
    mLeadAccountIds.put(l.domain__c,l.Lead_Accounts__c);
  }

  // Update triggered tasks with Account Id
  for(Lead t :trigger.new) {
    if(mLeadAccountIds.containsKey(t.domain__c) && t.email != null && t.Lead_Accounts__c == null && t.domain__c != 'outlook.com' && t.domain__c != 'gmail.com' && t.domain__c != 'yahoo.com' && t.domain__c != 'hotmail.com' && t.domain__c != 'googlemail.com' && t.domain__c != 'aol.com' && t.domain__c != 'mail.com' && t.domain__c != 'mail2web.com' && t.domain__c != 'mailinator.com' && t.domain__c != '10minutemail.com' && t.domain__c != '123vn.com') {
      t.Lead_Accounts__c = mLeadAccountIds.get(t.domain__c);
      t.Lead_Account_Attachment_Audit_Key__c = 'Attached';
          }
     if(t.email != null && t.Lead_Accounts__c == null && t.domain__c != 'outlook.com' && t.domain__c != 'gmail.com' && t.domain__c != 'yahoo.com' && t.domain__c != 'hotmail.com' && t.domain__c != 'googlemail.com' && t.domain__c != 'aol.com' && t.domain__c != 'mail.com' && t.domain__c != 'mail2web.com' && t.domain__c != 'mailinator.com' && t.domain__c != '10minutemail.com' && t.domain__c != '123vn.com') {
     Account a = new account();
     a.Name = t.company+' ('+t.domain__c+')';
     a.Website = t.domain__c;
     a.Created_From_Lead_Account_Attach_Trigger__c = 'Created from Trigger';
     insert a;
     
     t.Lead_Accounts__c = a.id;
    
  }
  }

}
Class:
@isTest
public class attachLeadToAccount1_Test
{    
      public static testmethod void testinsert()
      {

Account account= new account();
account.Name = 'United States Government';
account.OwnerID = '005d0000003Xjjh';
account.Website = 'hopethisworks.com';
account.recordtypeid = '012d0000000PBVJ';
insert account;

Lead lead= new lead();
lead.Firstname = 'Barack';
lead.Lastname = 'Obama';
lead.email ='barackyoursoxoff@usgov.com';
lead.company = 'United States Government';
lead.Lead_Accounts__c = [Select id from Account where Name = 'United States Government' limit 1].id;
insert lead;
            
Lead lead1= new lead();
lead1.Firstname = 'Michelle';
lead1.Lastname = 'Obama';
lead1.email ='baracksboss@usgov.com';
lead1.company = 'United States Government';
insert lead1;   

Lead lead2= new lead();
lead2.Firstname = 'Malaka';
lead2.Lastname = 'Obama';
lead2.email ='barackyoursoxoff1@gmail.com';
lead2.company = 'United States Government';
insert lead2;

Lead lead3= new lead();
lead3.Firstname = 'Mama';
lead3.Lastname = 'Obama';
lead3.email ='barackyoursoxoff1@newaccounttest.com';
lead3.company = 'New Account Test';
insert lead3;               

}
}