Executes a query against the specified object and returns data that matches the specified criteria.
QueryResult = sfdc.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(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());
}
}
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");
}
}
| 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). |