+ Start a Discussion
Bhushan burujwaleBhushan burujwale 

How to modify the chunk size for a Query? (default we get 2000 results)

I want to modify the chunk size of the result of the query. I have tried with adding the LIMIT but with this i cannot get the next chunk of the data.
I am using the salesforce stub to execute the query and if i use the LIMIT in the query the "isDone" parameter is true in the query result. I check the isDone parameter for getting next chunk using "queryMore" as below. 

public void queryRecords() {
02   QueryResult qResult = null;
03   try {
04      String soqlQuery = "SELECT FirstName, LastName FROM Contact";
05      qResult = connection.query(soqlQuery);
06      boolean done = false;
07      if (qResult.getSize() > 0) {
08         System.out.println("Logged-in user can see a total of "
09            + qResult.getSize() + " contact records.");
10         while (!done) {
11            SObject[] records = qResult.getRecords();
12            for (int i = 0; i < records.length; ++i) {
13               Contact con = (Contact) records[i];
14               String fName = con.getFirstName();
15               String lName = con.getLastName();
16               if (fName == null) {
17                  System.out.println("Contact " + (i + 1) + ": " + lName);
18               } else {
19                  System.out.println("Contact " + (i + 1) + ": " + fName
20                        + " " + lName);
21               }
22            }
23            if (qResult.isDone()) {
24               done = true;
25            } else {
26               qResult = connection.queryMore(qResult.getQueryLocator());
27            }
28         }
29      } else {
30         System.out.println("No records found.");
31      }
32      System.out.println("\nQuery succesfully executed.");
33   } catch (ConnectionException ce) {
34      ce.printStackTrace();
35   }
36}


I want to set the chunk cize to less than 2000, reason being, the data on the salesforce is huge and with default 2000 chunk size i get the few chunks of result and then i get the below error.

"QUERY_TIMEOUT: Your query request was running for too long"
Bhushan burujwaleBhushan burujwale
FYI i get query timeout for the following query.
"SELECT UserOrGroupId,Group.Name FROM GroupMember  order by UserOrGroupId"
Lokesh KumarLokesh Kumar
Try to have your query like below in a for-each manner.
 
// Use this format if you are not executing DML statements 
// within the for loop
for (Account a : [SELECT Id, Name FROM Account 
                  WHERE Name LIKE 'Acme%']) {
    // Your code without DML statements here
}

// Use this format for efficiency if you are executing DML statements 
// within the for loop
for (List<Account> accts : [SELECT Id, Name FROM Account
                            WHERE Name LIKE 'Acme%']) {
    // Your code here
    update accts;
}
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm