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

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

C# Example

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