The following trigger works well in sandbox, is validated in production, but when I used email to salesforce, it returns the following:
UnexpectedException: MoveAtt: execution of AfterInsert 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.MoveAtt: line 9, column 1
Trigger is as follows
trigger MoveAtt on Attachment (after insert) {
List<Id> forDeletionIds = new List<Id>();
for (Attachment a : trigger.new){
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == '00T'){
System.debug(a.parentId);
if(Task.WhoId != null){
if(Customer_Document__c.Task_ID__c != null){
Customer_Document__c parent = [SELECT Id FROM Customer_Document__c WHERE Task_ID__c = :a.ParentId Limit 1 ];
if (parent.Id != null){
Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
Attachment newA = New Attachment(
Name = a.Name,
Body = body.Body,
Description = 'Email Attachment from ' + date.today(),
OwnerId = a.OwnerId,
ParentId = parent.Id
);
insert newA;
forDeletionIds.add(a.Id);
}
}
}
}
List<Attachment> forDeletion = [SELECT Id FROM Attachment WHERE Id IN :forDeletionIds];
delete forDeletion;
}
}
How can I prevent looking for nulls? I don´t know, which Objects causes the 100000 rows.
Customer_Document__c parent = [SELECT Id FROM Customer_Document__c WHERE Task_ID__c = :a.ParentId Limit 1 ];
. Presumably your production org has more than 100,000Customer_Document__c
records in it?