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
KR_ForceKR_Force 

System.QueryException: Non-selective query against large object type (more than 100000 rows)

Can someone please help with this error?
An Error occured at trigger XXXXXXX on contact: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)

Here is my code, im calling this method from before insert and before update trigger.

*****ANY HELP WILL BE MUCH APPRICITED*******
  1.     public static void ContactProductSubscription(List<Contact>newcontacts,set<ID>acIds){
  2.         //set<id>acIds=new set<Id>();
  3.         set<id>acIds1=new set<Id>();
  4.         list<Asset>ast=new List<Asset>();
  5.         List<Linked_Account__c>lActs=new List<Linked_Account__c>();
  6.         List<Linked_Account__c>linkedAccounts=new List<Linked_Account__c>();
  7.         Map<Id,Id>actLActMap=new Map<Id,Id>();
  8.         Map<Id,Id>agentctLActMap=new Map<Id,Id>();
  9.         Map<Id,ID>alActs=new Map<Id,ID>();
  10.         Map<Id,Id>conActMap=new Map<Id,Id>();
  11.         Map<ID,String>ActPfamilyMap=new Map<ID,String>();
  12.         Map<ID,ID>lActAgentcontMap=new Map<ID,ID>();
  13.         Map<ID,ID>lActAcontMap=new Map<ID,ID>();
  14.         Map<ID,Asset>actAstMap=new Map<ID,Asset>();
  15.         set<id>aContacts=new set<id>();//Agent Contacts
  16.         set<string>pFamily=new set<string>();//Agent Contacts
  17.         String psubscription;
  18.          for(contact c :newcontacts){conActMap.put(c.id,c.accountID);}
  19.             if(acids.size()>0){
  20.                 lActs=[select Active__c, Agent__c, Name, Agent_Contact__c,Agent_Contact__r.account.ID,Agent_Contact_AccountId__c, Assets__c,
  21.                 Cases__c, Contacts__c, Expiration__c, Linked_Account__c, Id, User_Account__c, User_Contact__c FROM Linked_Account__c
  22.                 where Agent__c=true and  Contacts__c=true and (Expiration__c<>null OR Expiration__c >: date.today()) and Linked_Account__c in:acids ];
  23.             }
  24.             if(lActs.size()>0){
  25.             for(Linked_Account__c l:lActs){
  26.                 aContacts.add(l.Agent_Contact__c);
  27.                 actLActMap.put(l.Linked_Account__c,l.id);
  28.                 agentctLActMap.put(l.Agent_Contact__c,l.id);
  29.               
  30.                 }
  31.             } 
  32.             if(aContacts.size()>0){
  33.                 linkedAccounts=[select Active__c, Agent__c, Name, Agent_Contact__c,Agent_Contact__r.account.ID,Agent_Contact_AccountId__c, Assets__c,
  34.                 Cases__c, Contacts__c, Expiration__c, Linked_Account__c, Id, User_Account__c, User_Contact__c FROM Linked_Account__c
  35.                 where Agent__c=false and  isDeleted=false and Agent_Contact__c in:aContacts];
  36.             }
  37.             if(linkedAccounts.size()>0){
  38.             For(Linked_Account__c l1:linkedAccounts){
  39.                 acIds1.add(l1.Linked_Account__c);
  40.                 lActAgentcontMap.put(l1.Linked_Account__c,l1.Agent_Contact__c);
  41.                 lActAcontMap.put(l1.id,l1.Linked_Account__c);
  42.                 //agentctLActMap.put(l1.Agent_Contact__c,l1.id);
  43.           }
  44.             }
  45.             acIds1.addAll(acIds);
  46.             if(acIds1.size()>0){ast=[select AccountId, Id, Product_Family__c FROM Asset where AccountId in:acIds1];}
  47.             for(Asset a:ast){ActPfamilyMap.put(a.AccountId,a.Product_Family__c);
  48.             actAstMap.put(a.AccountId,a);}
  49.             for(contact c :newcontacts){
  50.             Id lactID=actLActMap.get(c.AccountID);
  51.             ID aContID=lActAgentcontMap.get(lactID);
  52.             ID lactID1=agentctLActMap.get(aContID);
  53.             ID ActID=lActAcontMap.get(lactID1);
  54.             if(ActPfamilyMap.containsKey(ActID)||ActPfamilyMap.containsKey(c.AccountID)){
  55.             for(Asset a1:[select AccountId, Id, Product_Family__c FROM Asset where AccountId =: c.AccountID OR AccountId=:ActID]){
  56.              psubscription+=a1.Product_Family__c+'; ';
  57.              c.productSubscriptions__c=psubscription;
  58.             }
  59.         
  60.             }
  61.          }
  62.       }
Best Answer chosen by KR_Force
KR_ForceKR_Force
I was able to resolve the issue on my own..@Ramesh thanks for your response.

All Answers

Ramesh KallooriRamesh Kalloori
please go through the below link.

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm
KR_ForceKR_Force
I was able to resolve the issue on my own..@Ramesh thanks for your response.
This was selected as the best answer