Activity Forums Salesforce® Discussions What is System.LimitException: Too many query rows: 50001 in Apex and Triggers?

  • What is System.LimitException: Too many query rows: 50001 in Apex and Triggers?

    Posted by Prachi on September 10, 2018 at 2:36 pm

    What is System.LimitException: Too many query rows: 50001 in Apex and Triggers? what does it mean and how to resolve it?

    shariq replied 4 years ago 3 Members · 2 Replies
  • 2 Replies
  • Anurag

    Member
    September 11, 2018 at 6:55 am

    HI Prachi,

    There is a limit to the number of records that that can be retrieved  by the SOQL Query which is 50000 records. Attempting to query the records having the number more than this will give the error which you are getting.To query the records more than its limit, you need to use a Batch Class which provides a fresh limit of 50k records by resetting the limits.

  • shariq

    Member
    September 11, 2018 at 2:18 pm

    Hi,

    Because Apex runs in a multitenant environment, the Apex runtime engine strictly enforces limits to ensure that runaway Apex code or processes don’t monopolize shared resources. If some Apex code exceeds a limit, the associated governor issues a runtime exception that cannot be handled. These limits count for each Apex transaction. For Batch Apex, these limits are reset for each execution of a batch of records in the execute method.

    In a SOQL query with parent-child relationship subqueries, each parent-child relationship counts as an extra query. These types of queries have a limit of three times the number for top-level queries. The limit for subqueries corresponds to the value that Limits.getLimitAggregateQueries() returns.The row counts from these relationship queries contribute to the row counts of the overall code execution. This limit doesn’t apply to custom metadata types. In a single Apex transaction, custom metadata records can have unlimited SOQL queries. In addition to static SOQL statements, calls to the following methods count against the number of SOQL statements issued in a request.Database.countQuery
    Database.getQueryLocator
    Database.query

    Hope this helps!

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos