queryMore()

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

Syntax

QueryResult = sfdc.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 for more information.

Note
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.

Sample Code—Java

private void querySample() {
  QueryResult qr = null;
  QueryOptions qo = new QueryOptions();
  qo.setBatchSize(new Integer(250));
  binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), 
                     "QueryOptions", qo);
  try {
    qr = binding.query("select FirstName, LastName from Contact");
    boolean done = false;
    if (qr.getSize() > 0){
      System.out.println("Logged-in user can see " + qr.getRecords().length + 
                         " contact records.");
      while (!done) {
        for (int i=0;i<qr.getRecords().length;i++) {
          Contact con = (Contact)qr.getRecords(i);
          String fName = con.getFirstName();
          String lName = con.getLastName();
          if (fName == null)
            System.out.println("Contact " + (i + 1) + ": " + lName);
          else
            System.out.println("Contact " + (i + 1) + ": " + fName + " " + lName);
        }
        if (qr.isDone()) {
          done = true;
        } else {
          qr = binding.queryMore(qr.getQueryLocator());
        }
      }
    }
    else {
      System.out.println("No records found.");
    }
    System.out.println("\nQuery succesfully executed.");
  }
  catch (RemoteException ex) {
    System.out.println("\nFailed to execute query succesfully, error message was: \n" + 
                       ex.getMessage());
  }
}

Sample Code—C#

using System;
using System.Collections.Generic;
using System.Text;
using SFDCWsdl.Salesforce_WSDL;

namespace SFDCWsdl
{
    public class SFDCWsdlTest
    {
        private SforceService binding;

        private static readonly string Username = "USERNAME";
        private static readonly string Password = "PASSWORD";

        /// <summary>
        /// Create the binding and login
        /// </summary>
        private SFDCWsdlTest()
        {
            this.binding = new SforceService();
            binding.login(SFDCWsdlTest.Username, SFDCWsdlTest.Password);
        }

        private void QuerySample()
        {
            QueryResult qr = null;

            binding.QueryOptionsValue = new QueryOptions();
            binding.QueryOptionsValue.batchSize = 250;
            binding.QueryOptionsValue.batchSizeSpecified = true;


            qr = binding.query("select FirstName, LastName from Contact");

            bool bContinue = true;
            int loopCounter = 0;
            while (bContinue)
            {
                Console.WriteLine("\nResults Set " + Convert.ToString(loopCounter++)
                           + " - ");
                //process the query results
                for (int i = 0; i < qr.records.Length; i++)
                {
                    sObject con = qr.records[i];
                    string fName = con.Any[0].InnerText;
                    string lName = con.Any[1].InnerText;
                    if (fName == null)
                        Console.WriteLine("Contact " + (i + 1) + ": " + lName);
                    else
                        Console.WriteLine("Contact " + (i + 1) + ": " + fName
                                   + " " + lName);
                }
                //handle the loop + 1 problem by checking the most recent queryResult
                if (qr.done)
                    bContinue = false;
                else
                    qr = binding.queryMore(qr.queryLocator);
            }
            Console.WriteLine("\nQuery succesfully executed.");
            Console.Write("\nHit return to continue...");
            Console.ReadLine();
        }
    }
}

Arguments

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

Response

QueryResult

Faults

InvalidQueryLocatorFault

UnexpectedErrorFault

See Also:
query()
API Call Basics
Changing the Batch Size in Queries
https://wiki.apexdevnet.com/index.php/Sample_SOAP_Messages
© Copyright 2000-2008 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.