Activity Forums Salesforce® Discussions How to Fetch more than 50k records using SOQL in Salesforce?

  • Manpreet

    April 26, 2018 at 4:32 pm

    Hi saurabh,

    Normally, queries for a single Visualforce page request may not retrieve more than 50,000 rows. In read-only mode, this limit is relaxed to allow querying up to 1 million rows.

    <apex:page controller="SummaryStatsController" readOnly="true">
    <p>Here is a statistic: {!veryLargeSummaryStat}</p>

    The controller for this page is also simple, but illustrates how you can calculate summary statistics for display on a page:

    public class SummaryStatsController
    public Integer getVeryLargeSummaryStat()
    Integer closedOpportunityStats =
    [SELECT COUNT() FROM Opportunity WHERE Opportunity.IsClosed = true];
    return closedOpportunityStats;


  • Aman

    April 26, 2018 at 8:27 pm

    Hi Saurabh,

    You should look at using Batch Apex to accomplish your goals.

    You cannot retrieve more than 50,000 records your SOQL calls in a single context.

    However, with  Batch Apex your logic will be processed in chunks of anywhere from 1 to 200 records in a batch.

    You'd need to modify your business logic to take the batching into account if necessary.


Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos