Executes a query against the specified object and returns data that matches the specified criteria.
QueryResult = binding.query(string queryString);
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 Salesforce Object Query Language (SOQL).
Upon invocation, the API executes the query against the specified object, caches the results of the query on the API, 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. To determine whether an object can be queried, your client application can invoke the describeSObjects() call on the object and inspect its queryable property.
You can use queryAll() to query on all Task and Event records, archived or not. You can also filter on the isArchived field to find only the archived objects. You cannot use query(), it automatically filters out all records where isArchived is set to true. You can insert, update, or delete archived records.
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 API returns an API fault element of InvalidQueryLocatorFault. If a timeout occurs, refactor your query to return or scan a smaller amount of data.
When querying for fields of type Base64 (see base64), the query response object returns only one record at a time. You cannot alter this by changing the batch size of the query() call.
private void querySample() {
QueryResult qr = null;
_QueryOptions qo = new _QueryOptions();
qo.setBatchSize(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());
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services.Protocols;
using Walkthrough.sforce;
namespace SFDCWsdl
{
public class SFDCWsdlTest
{
private SforceService binding;
private static readonly string Username = "ENTERUSERNAME";
private static readonly string Password = "ENTERPASSWORD";
/// <summary>
/// Create the binding and login
/// </summary>
private SFDCWsdlTest()
{
this.binding = new SforceService();
LoginResult lr = binding.login(SFDCWsdlTest.Username, SFDCWsdlTest.Password);
this.binding.Url = lr.serverUrl;
this.binding.SessionHeaderValue = new SessionHeader();
this.binding.SessionHeaderValue.sessionId = lr.sessionId;
}
public 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++)
{
Contact con = (Contact)qr.records[i];
string fName = con.FirstName;
string lName = con.LastName;
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();
}
[STAThread]
static void Main(string[] args)
{
SFDCWsdlTest sfdcWsdlTest = new SFDCWsdlTest();
sfdcWsdlTest.QuerySample();
}
}
}
| 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 Salesforce Object Query Language (SOQL). |