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
Anu-SFDCAnu-SFDC 

Custom Indexing Error

Hi,

 

I have deployed a package.. For most of the users it is working fine.., but one of my customer having this problem

 

 

Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CnP.addCorresCont: 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)

 

 

Before the release it is working fine,.

 

Here is the code that i changed in this release..

 

 

 

  set<String> EmailSet1= new set<String>();
     set<String> CampaignList = new set<String>();
     set<String> SfCampaignList = new set<String>();
     map<String, String> CampaignMap = new map<String, String>();
     map<String, String> SfCampaignMap = new map<String, String>();
     map<String, Id> Map_Contact_To_Id = new map<String, Id>();
     map<String, ID> Map_CampaignName_To_Id = new map<String, ID>();
     map<Id,Id>  Map_CampaignId_To_Id = new map<Id,Id>();    
    
     for (CnP_Transaction__c c : Trigger.new) {
         CampaignList.add(c.Campaign__c);
         SfCampaignList.add(c.sf_Campaign__c);
         CampaignMap.put(c.Campaign__c,c.Campaign__c);
         SfCampaignMap.put(c.sf_Campaign__c,c.sf_Campaign__c);
         EmailSet1.add(c.Email__c);
     }
      list<Contact> ContactMap1=[Select Id,Email,npo02__Household__c from Contact where Email IN :EmailSet1];
       for(Contact k:ContactMap1){
          Map_Contact_To_Id.put(k.Email,k.Id);
        }
      list<Campaign> theCampaign = [SELECT Id, Name FROM Campaign WHERE Name IN :CampaignList OR Id IN :SfCampaignList];
     
      for (Campaign k:theCampaign) {
       Map_CampaignName_To_Id.put(k.Name,k.Id);
       Map_CampaignId_To_Id.put(k.Id,k.Id);
      }

      for (CnP_Transaction__c c : Trigger.new) {
      if(c.sf_Campaign__c == null)
          c.sf_Campaign__c=Map_CampaignName_To_Id.get(c.Campaign__c);
      }
      list<CampaignMember> CheckCamMem=[select contact.Email,Campaignid,Contactid from CampaignMember];
      Map<ID,ID> Map_CamId_ConId=new map<ID,ID>();
    
      for(CampaignMember m : CheckCamMem){
         Map_CamId_ConId.put(m.Campaignid,m.Contactid);
      }
      list<CampaignMember> ListMemebers=new list<CampaignMember>();
           CampaignMember CreateMem=new CampaignMember();
 
    for (CnP_Transaction__c c : Trigger.new) {
     if((c.sf_Campaign__c!=null || c.Campaign__c!= null)&&Map_CamId_ConId.get(c.sf_Campaign__c)!=c.Contact__c){
        CreateMem = new CampaignMember(Campaignid=Map_CampaignId_To_Id.get(c.sf_Campaign__c), Contactid=c.Contact__c, Status='Received');
          Map_CamId_ConId.put(Map_CampaignId_To_Id.get(c.sf_Campaign__c),Map_Contact_To_Id.get(c.Email__c));
          ListMemebers.add(CreateMem);
     }
    }

     if(ListMemebers.size()>0){
       insert ListMemebers;      
       }
    
  }

 

 

Plz help me

 

 

 

 

SteveBowerSteveBower

I haven't had this particular error before, but I'm guessing that it's merely telling you that you're query is returning more rows than is feasible.  Not the total number of rows, but just that they can't be efficiently processed?  

 

Can you break the Campaign Select into two queries and process two lists?   

 

Also, you might:

 

1. Get rid of the redundant Sets... for example:

 set<String> SfCampaignList = new set<String>();

  map<String, String> SfCampaignMap = new map<String, String>();
    
     for (CnP_Transaction__c c : Trigger.new) {
         SfCampaignList.add(c.sf_Campaign__c);
         SfCampaignMap.put(c.sf_Campaign__c,c.sf_Campaign__c);

...   

and later when you use sfCampaignList in an "in" clause in a query you could instead use:  SfCampaignMap.keySet()  which gives you what you want.

 

2. You have two lines of code:

         CampaignMap.put(c.Campaign__c,c.Campaign__c);

and 

       Map_CampaignId_To_Id.put(k.Id,k.Id);

which don't make sense.  There is rarely any reason to map from something to itself.  So, I'm thinking something is wrong here.

 

3. May I suggest you add some comments... it's hard to see exactly what you're trying to do here.

 

4. Consider using Bulk safe For loops...  I'm not sure it's totally relevant in this case, but it's good practice.  Look for "Working with Very Large SOQL Queries" in the Apex doc.

 

Best, Steve.

Anu-SFDCAnu-SFDC

Hi Steve,

 

Thanks for the reply..

 

For the second point,  i have removed the usage of those maps but forgot to remove the definitions.. I dont think it is a bug..

 

 

My client has 1000000 records in his instance.. I think the problem is with this line,

 

  list<CampaignMember> CheckCamMem=[select contact.Email,Campaignid,Contactid from CampaignMember];

 

 

Here dont have where clause and user have almost 500000 records in campaign members.. How can I solve this?

TejTej

I think u need to have a where condition and that field needs to be set as an external id.

Anu-SFDCAnu-SFDC

Hi,

 


      list<CampaignMember> CheckCamMem=[select contact.Email,Campaignid,Contactid from CampaignMember where contact.Email!=null];

 

But contact.Email in Campaign is standard field. how can i make that field as external id?

 

 

Anu