Newer Version Available

This content describes an older version of this product. View Latest

queryMore()

Retrieves the next batch of objects from a query().

Syntax

1QueryResult = connection.queryMore( QueryLocator  QueryLocator);

Usage

Use this call to process query() calls that retrieve a large number of records (by default, more than 500) in the result set. The query() call retrieves the first 500 records and creates a server-side cursor that is represented in the queryLocator object. The queryMore() call processes subsequent records in up to 500-record chunks, resets the server-side cursor, and returns a newly generated QueryLocator. To iterate through records in the result set, you generally call queryMore() repeatedly until all records in the result set have been processed (the Done flag is true). You can change the maximum number of records returned to up to 2,000. See Changing the Batch Size in Queries in the Salesforce SOQL and SOSL Reference Guide for more information.

A queryMore()call on a parent object invalidates all child cursors in the previous result set. If you need the results from the child, you must use queryMore() on those results before using queryMore() on the parent results.

Note

You can't use queryMore() if a query includes a GROUP BY clause. See GROUP BY and queryMore() in the Salesforce SOQL and SOSL Reference Guide for more information.

Sample Code—Java

This sample executes a query that fetches the first names and last names of all contacts. It calls query() with the query string to get the first batch of records. It then calls queryMore() in a loop to get subsequent batches of records until no records are returned. It writes the first and last names of the contacts queried to the console.

1swfobject.registerObject("clippy.codeblock-1", "9");public void queryRecords() {
2   QueryResult qResult = null;
3   try {
4      String soqlQuery = "SELECT FirstName, LastName FROM Contact";
5      qResult = connection.query(soqlQuery);
6      boolean done = false;
7      if (qResult.getSize() > 0) {
8         System.out.println("Logged-in user can see a total of "
9            + 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}

Sample Code—C#

This sample executes a query that fetches the first names and last names of all contacts. It calls query() with the query string to get the first batch of records. It then calls queryMore() in a loop to get subsequent batches of records until no records are returned. It writes the first and last names of the contacts queried to the console.

1swfobject.registerObject("clippy.codeblock-2", "9");public void queryRecords()
2{
3   QueryResult qResult = null;
4   try
5   {
6      String soqlQuery = "SELECT FirstName, LastName FROM Contact";
7      qResult = binding.query(soqlQuery);
8      Boolean done = false;
9      if (qResult.size > 0)
10      {
11         Console.WriteLine("Logged-in user can see a total of "
12            + qResult.size + " contact records.");
13         while (!done)
14         {
15            sObject[] records = qResult.records;
16            for (int i = 0; i < records.Length; ++i)
17            {
18               Contact con = (Contact)records[i];
19               String fName = con.FirstName;
20               String lName = con.LastName;
21               if (fName == null)
22               {
23                  Console.WriteLine("Contact " + (i + 1) + ": " + lName);
24               }
25               else
26               {
27                  Console.WriteLine("Contact " + (i + 1) + ": " + fName
28                        + " " + lName);
29               }
30            }
31            if (qResult.done)
32            {
33               done = true;
34            }
35            else
36            {
37               qResult = binding.queryMore(qResult.queryLocator);
38            }
39         }
40      }
41      else
42      {
43         Console.WriteLine("No records found.");
44      }
45      Console.WriteLine("\nQuery succesfully executed.");
46   }
47   catch (SoapException e)
48   {
49      Console.WriteLine("An unexpected error has occurred: " +
50                                 e.Message + "\n" + e.StackTrace);
51   }
52}

Arguments

Name Type Description
queryLocator QueryLocator Represents the server-side cursor that tracks the current processing location in the query result set.

Response

QueryResult