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
Ivan WinzerIvan Winzer 

Non-selective query against large object type (more than 100000 rows)

Ok so i have read other entries but nothing i take seems to be working so hopefully someone can look at my code and see what the issue is. Im getting these emails raipdly these days and even when going to clean up data im starting to see if on new records as well.

PrimaryAddressValidation: 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.PrimaryAddressValidation: line 55, column 1

and this is my code the error is coming in at the fist List select statment in each if statement below.
 
// section makes sure that only one checkbox is true within the related address list for the contact
      if(!contactIdsForShipping.isEmpty())
      {
          List<ShipTo_Address__c> toggleShippings = shipToExcluded.isEmpty()?
             [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true]
             : [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true AND Id NOT IN :shipToExcluded];

          for(ShipTo_Address__c a : toggleShippings)
          {
                  a.Default_Shipping_Address__c = false;
          }
          update toggleShippings;
      }

      if(!contactIdsForBilling.isEmpty())
          {
              List<ShipTo_Address__c> toggleBilling = billToExcluded.isEmpty()?
              [SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true]
              :[SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true AND Id NOT IN :billToExcluded];
              for(ShipTo_Address__c a : toggleBilling)
              {
                  a.Primary_Billing_Address__c = false;
              }
              update toggleBilling;
          }   

    }

 
Best Answer chosen by Ivan Winzer
Ketankumar PatelKetankumar Patel
Hi Ivan, 

You should try make Contact__c as external Id field that would resove this error if you can't do that then you should try filtering out null from your query. you should do that for all your queries. 
[SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true AND Contact__c ! = NULL]
Here is the same solved question: https://developer.salesforce.com/forums/?id=906F00000008vLPIAY
 

All Answers

Chandra Sekhar CH N VChandra Sekhar CH N V
How many rows does the query return? It should be >50000 and which is why I feel it is giving you the exception.
Ketankumar PatelKetankumar Patel
Hi Ivan, 

You should try make Contact__c as external Id field that would resove this error if you can't do that then you should try filtering out null from your query. you should do that for all your queries. 
[SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true AND Contact__c ! = NULL]
Here is the same solved question: https://developer.salesforce.com/forums/?id=906F00000008vLPIAY
 
This was selected as the best answer
Ivan WinzerIvan Winzer
Thanks Ketankumar that worked. I couldnt change the contact to be external for some reason by putting in the Contct__c != Null has stopped the emails from coming when the updates run throughout the day.

Thanks Again your awesome....

Ivan