Newer Version Available

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

Sample query and queryMore Calls

The following Java and C# examples show usage of the query() and queryMore() calls for the partner WSDL. Each example sets the batch size of the query to 250 items returned. It then performs a query call to get the first name and last name of all contacts and iterates through the contact records returned. For each contact, it writes the contact’s first name and last name to the output, or only the last name if the first name is null. Finally, if there are more items to be returned by the query, as indicated by a QueryResult.done property value of false, it calls queryMore() to get the next batch of items, and repeats the process until no more records are returned.

To execute the sample method, you can use the corresponding Java or C# template class provided in Examples Using the Partner WSDL.

Java Example

1public void querySample() {    
2    try {
3        // Set query batch size
4        partnerConnection.setQueryOptions(250);
5        
6        // SOQL query to use 
7        String soqlQuery = "SELECT FirstName, LastName FROM Contact";
8        // Make the query call and get the query results
9        QueryResult qr = partnerConnection.query(soqlQuery);
10        
11        boolean done = false;
12        int loopCount = 0;
13        // Loop through the batches of returned results
14        while (!done) {
15            System.out.println("Records in results set " + loopCount++
16                    + " - ");
17            SObject[] records = qr.getRecords();
18            // Process the query results
19            for (int i = 0; i < records.length; i++) {
20                SObject contact = records[i];
21                Object firstName = contact.getField("FirstName");
22                Object lastName = contact.getField("LastName");
23                if (firstName == null) {
24                    System.out.println("Contact " + (i + 1) + 
25                            ": " + lastName
26                    );
27                } else {
28                    System.out.println("Contact " + (i + 1) + ": " + 
29                            firstName + " " + lastName);
30                }
31            }
32            if (qr.isDone()) {
33                done = true;
34            } else {
35                qr = partnerConnection.queryMore(qr.getQueryLocator());
36            }
37        }
38    } catch(ConnectionException ce) {
39        ce.printStackTrace();
40    }
41    System.out.println("\nQuery execution completed.");         
42}

C# Example

1public void querySample() 
2{
3    try
4    {
5        QueryResult qr = null;
6        binding.QueryOptionsValue = new sforce.QueryOptions();
7        binding.QueryOptionsValue.batchSize = 250;
8        binding.QueryOptionsValue.batchSizeSpecified = true;
9
10        qr = binding.query("SELECT FirstName, LastName FROM Contact");
11
12        bool done = false;
13        int loopCount = 0;
14        while (!done)
15        {
16            Console.WriteLine("\nRecords in results set " + 
17                Convert.ToString(loopCount++)
18                    + " - ");
19            // Process the query results
20            for (int i = 0; i < qr.records.Length; i++)
21            {
22                sforce.sObject con = qr.records[i];
23                string fName = con.Any[0].InnerText;
24                string lName = con.Any[1].InnerText;
25                if (fName == null)
26                    Console.WriteLine("Contact " + (i + 1) + ": " + lName);
27                else
28                    Console.WriteLine("Contact " + (i + 1) + ": " + fName
29                        + " " + lName);
30            }
31
32            if (qr.done)
33                done = true;
34            else
35                qr = binding.queryMore(qr.queryLocator);
36        }
37    }
38    catch (SoapException e)
39    {
40        Console.WriteLine("An unexpected error has occurred: " + e.Message +
41            " Stack trace: " + e.StackTrace);
42    }
43    Console.WriteLine("\nQuery execution completed.");
44}