Activity › Forums › Salesforce® Discussions › How to query records from sObject for 50000+ records?
Tagged: Batch Class, Controller Class, Read Only, Salesforce Apex, Salesforce Records, Salesforce sObject, Salesforce SOQL, Salesforce Visualforce Page
-
How to query records from sObject for 50000+ records?
Posted by shafali on April 28, 2016 at 10:44 AMHow to query (without using batch class) records from sObject if there are more than 50,000 records for the same?
shariq replied 7 years, 8 months ago 5 Members · 5 Replies -
5 Replies
-
You can go through this link hopw it would be helpul :
https://force746.wordpress.com/2014/08/28/count-large-number-of-records-more-than-50000/
- [adinserter block='9']
-
There two more methods to query data more then 50000
Method 1: Use Visualforce page with readOnly attribute set to true
Controller class:public class StatsController {
public Integer numberOfContacts {
get {
if (numberOfContacts == null) {
numberOfContacts = [select count() from Contact];
}
return numberOfContacts;
}
private set;
}
}
Visualforce page:<apex:page controller=”StatsController” readOnly=”true”>
<p>Number of Contacts: {!numberOfContacts}</p>
</apex:page>Method 2: Make a http request using the REST API
HttpRequest req = new HttpRequest();
req.setEndpoint(‘https://’+URL.getSalesforceBaseUrl().getHost()+’/services/data/v20.0/query/?q=SELECT+Id+from+Contact’);
req.setMethod(‘GET’);string autho = ‘Bearer ‘+ userInfo.getsessionId();
req.setHeader(‘Authorization’, autho);Http http = new Http();
HTTPResponse res = http.send(req);
string response = res.getBody();
string total = response.substring(response.indexOf(‘totalSize”:’) + 11, response.indexOf(‘,’));
system.debug(‘Total: ‘+ total); -
Hi,
You can use Batchable class:
public class ContactBatchable implements Database.Batchable<sObject>, Database.Stateful {
Integer total = 0;public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(‘select Id from Contact’);
}public void execute(
Database.BatchableContext BC,
List<sObject> scope){
total += scope.size();
}public void finish(Database.BatchableContext BC){
System.debug(‘total: ‘ + total);
}
}And then execute the following statement in developer console:
Database.executeBatch(new ContactBatchable(), 2000);
Thanks.
-
Hi,
You can try this –
controller –
public class StatsController {
public Integer numberOfContacts {
get {
if (numberOfContacts == null) {
numberOfContacts = [select count() from Contact];
}
return numberOfContacts;
}
private set;
}
}vf page –
<apex:page controller=”StatsController” readOnly=”true”>
<p>Number of Contacts: {!numberOfContacts}</p>
</apex:page>Hope this helps.
Log In to reply.