I have a trigger in opportunity in trigger on update and insert. It was working until today but suddenly gotta stuck. It is working fine while update but while inserting it is giving error like bellow

Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, xxx: 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) Trigger.xxx: line 97, column 1: [] (System Code)

  • Just as the error states, its likely because you have a query that is retrieving far to too many rows. You need to adjust your query. Can't help much without the code. Can you post the trigger. Commented Dec 12, 2013 at 12:53
  • While its clear this query will return quite a large number of records if you have lots of opportunities in your org, we need a bit of context, not just the query. Can you post the entire trigger, as well as what you are trying to accomplish with the trigger, this way, others can perhaps suggest a better way all together to accomplish what you are trying to do. You can edit the question above and add the code for the trigger. Commented Dec 12, 2013 at 12:57
  • You are right it is returning 2 lakhs records,How can i handle to prevent the error
    – Sunny
    Commented Dec 12, 2013 at 13:01
  • 3
    You need to add the code for the trigger in order for people to help you. Commented Dec 12, 2013 at 13:03

2 Answers 2


Look at Working with Very Large SOQL Queries. The error message sounds like you have a non-selective query and will need to rewrite the query.

If you cannot rewrite the query and still meet the business requirements you could move your logic into a Batch job and apply your filter in the execute method.

  • Yes i got your point and i placed limit as 1 in query ,but it is giving same error. one more thing is i have 4 trigger on same object....Need i put limit in all triggers?
    – Sunny
    Commented Dec 13, 2013 at 4:33
  • It is possible. Hard to tell without the code, like @sfdc_ninja stated. Commented Dec 13, 2013 at 17:52

Since the number of objects being inserted are large, you need to make the query 'Selective'. This error will usually come only if the number of records in the object is more that 100,000.

As mentioned in the error, this issue can be fixed by indexing the fiels in the where condition.

  • By default: Id, Name and Owner fields, lookup, master-detail fields, audit fields (like LastModifiedDate) and custom fields marked as External ID or Unique.
  • If your query has other fields in the where condition then raise a case with saleforce to index those fields.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .