AppExchange logo

query


Executes a query against the specified object and returns data that matches the specified criteria.

Syntax

QueryResult = sfdc.query(string queryString); 

Usage

Use the query call to retrieve data from an object. When a client application invokes the query call, it passes in a query expression that specifies the object to query, the fields to retrieve, and any conditions that determine whether a given object qualifies. For an extensive discussion about the syntax and rules used for queries, see Sforce Object Query Language (SOQL).

Upon invocation, the Web service executes the query against the specified object, caches the results of the query on the Web service, and returns a query response object to the client application. The client application can then use methods on the query response object to iterate through rows in the query response and retrieve information.

Your client application must be logged in with sufficient access rights to query individual objects within the specified object and to query the fields in the specified field list. For more information, see Factors that Affect Data Access.

Certain objects cannot be queried via the API. To query an object via the query call, its object must be configured as queryable (queryable is True). To determine whether an object can be queried, your client application can invoke the describeSObjects call on the object and inspect its queryable property.

Suggestion: If you use the Enterprise WSDL, you should not use describe to populate a select list. For example, if a system administrator adds a field to the WSDL after you consume it, the describe will pull down the field but your toolkit won't know how to serialize it, and your integration may fail.

The query response object contains up to 500 rows of data by default. If the query results exceed 500 rows, then the client application uses the queryMore call and a server-side cursor to retrieve additional rows in 500-row chunks. You can increase the default size up to 2,000 in the QueryOptions header, as described in Changing the Batch Size in Queries.

Queries that take longer than two minutes to process will be timed out. For timed out queries, the AppExchange Web service returns an ExceptionCode of QUERY_TIMEOUT. If a timeout occurs, refactor your query to return or scan a smaller amount of data.

When querying for fields of type Base64 (see base64Binary), the query response object returns only one record at a time. You cannot alter this by changing the batch size of the query call.

:: Note

For multi-currency organizations, special handling is required when querying currency fields containing values in different currencies. For example, if a client application is querying PricebookEntry objects based on values in the UnitPrice field, and if the UnitPrice amounts are expressed in different currencies, then the query logic must handle this case correctly. For example, if the query is trying to retrieve the product codes of all products with a unit price greater than or equal to $10USD. the query expression might look something like this:

select Product2Id,ProductCode,UnitPrice from PricebookEntry
where (UnitPrice >= 10 and CurrencyIsoCode="USD")
or (UnitPrice >= 5.47 and CurrencyIsoCode="GBP")
or (UnitPrice >= 8.19 and CurrencyIsoCode="EUR")

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#

private void contactQuery()  
{ 
   // Set the query options (Optional; default batch size is 2000) 
   binding.QueryOptionsValue = new QueryOptions(); 
   binding.QueryOptionsValue.batchSize = 500; 
   binding.QueryOptionsValue.batchSizeSpecified = true; 
    
   // Invoke the query call and save the result in a QueryResult 
   QueryResult qr = binding.query("select FirstName, LastName from contact where
MailingPostalCode = '94062'"); 
 
   // Get the returned records 
   sObject[] records = qr.records; 
 
   // Determine whether some records where returned 
   if (records.Length > 0)  
   { 
      bool done = false;  // Use this for loop control 
      while (done == false) // Or while(!done)  
      { 
         for (int i=0; i<records.Length; i++)  
         { 
            Contact contact = (Contact)records[i]; 
            System.Diagnostics.Trace.WriteLine(contact.FirstName + " " +
contact.LastName); 
         } 
         // Update the loop control 
         done = qr.done; 
         // Determine whether we need to retrieve another batch of result records 
         if (done == false)  
         {  
             qr = binding.queryMore(qr.queryLocator);  
             records = qr.records;  
         }  
    else  
        {  
            done = qr.done;  
       } 
   else 
   { 
      System.Diagnostics.Trace.WriteLine("no records matched criteria"); 
   } 
} 

Arguments

Name
Type
Description
queryString
string
Query string that specifies the object to query, the fields to return, and any conditions for including a specific object in the query. For more information, see Sforce Object Query Language (SOQL).

Response

QueryResult

Faults

MalformedQueryFault

InvalidSObjectFault

InvalidFieldFault

UnexpectedErrorFault

See Also

queryMore

Sforce Object Query Language (SOQL)

Using API Calls

Changing the Batch Size in Queries

Sample SOAP Messages - query


© Copyright 2000-2006 salesforce.com, inc.
All rights reserved • Various trademarks held by their respective owners.
Have feedback about this page? Click the red email icon in the top right corner to contact us.