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
ArrgHunterArrgHunter 

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

i am getting errro when trying to update child records in apex trigger

error details

ystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, trgAnnualProgressReportIns: 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.trgAnnualProgressReportIns: line 47, column 1: []
Stack Trace: Class.testtrgAnnualProgressReportInsMethods.testAnnualProgressReportIns: line 139, column 1


Code for Trigger.trgAnnualProgressReportIns: line 47, column 1

for(Annual_Progress_Report__c a:[select id,APR_Education_Current__c, APR_Living_Current__c,APR_Health_Current__c from Annual_Progress_Report__c where Status__c = 'Approved' order by name desc Limit 1])


code for  Class.testtrgAnnualProgressReportInsMethods.testAnnualProgressReportIns: line 139, column 1

Insert testAPR1;

any idea how can i tackle this issue
Grazitti TeamGrazitti Team
Hi ArrgHunter,

There may be one of the following reason for the issue-

1. Non-selective filter in the query:-

It seems that status__c is formula field or non-indexed. so , please try to add other filter as well as-

for(Annual_Progress_Report__c a:[select id,APR_Education_Current__c, APR_Living_Current__c,APR_Health_Current__c from Annual_Progress_Report__c where Status__c = 'Approved'  and Status__c != null AND (add other filter if you can) order by name desc])

2. If there are large number of data are coming through the query:-

In this case, handle the issue as following-
a). Put the code  @future method (if code is in the trigger then use @future (Callout=true)
OR
b). Implement a batch job
 

Please post the code if you face any other issue.

Regards,
Grazitti Team
Web: www.grazitti.com

shiv@SFDCshiv@SFDC
This link might help you

https://developer.salesforce.com/forums/ForumsMain?id=906F00000009Da4IAE
ArrgHunterArrgHunter
Thanks everyone for imput.

May be i am over complicating my requirement. what i want is,  when a user create a new record in custom object i want to copy 3 fields (integer values) from last saved record and insert to newly created one.

Any suggestions for easy solution.

my current solution works fine on sandbox but when i try to deploy it to live it throw above error.

thanks,