You are here

function salesforce_api_query in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 7.2 salesforce_api/salesforce_api.module \salesforce_api_query()

Wraps SforceBaseClient::query. Queries Salesforce for a record or set of records. For information about SOQL syntax,


string $query: A SOQL query string.

array $options.: An array of options for how the query should be done. Valid options are: queryAll (bool) Whether or not to include deleted records in this query. Default FALSE. queryMore (bool) Whether or not to include all records matching this query. Default FALSE. limit (integer) Set the SOQL Batch Size. This is NOT analagous to SOQL LIMIT (nor SQL LIMIT). Minimum value is 200. Maximum value is 2,000. Default varies. For more information about batch size, @see

object $sf: A Salesforce connection. If not set, this function will connect.

Return value

object An array of matching records on success, or FALSE on failure.

See also

1 call to salesforce_api_query()
_sf_import_get_soql_records in sf_import/sf_import.module
For the fieldmap provided, attempt to pull updated Salesforce IDs using the SOQL query defined for the map.


salesforce_api/salesforce_api.module, line 1345
Defines an API that enables modules to interact with the Salesforce server.


function salesforce_api_query($query, $options = array(), $sf = NULL) {

  // If not passed a Salesforce connection, then connect to Salesforce.
  if (!is_object($sf)) {
    $sf = salesforce_api_connect();

    // Return FALSE if could not connect to Salesforce.
    if ($sf == FALSE) {
      return FALSE;

  // Merge in defaults.
  $options += array(
    'queryAll' => FALSE,
    'queryMore' => FALSE,
    'limit' => NULL,

  // Set a limit for the query if requested.
  // @todo: Determine why this limit is not being applied.
  if (is_numeric($options['limit']) && $options['limit'] > 0) {
    if ($options['limit'] > 2000) {
      $options['limit'] = 2000;
    if ($options['limit'] < 200) {
      $options['limit'] = 200;
    $queryOptions = new QueryOptions($options['limit']);

  // Execute the query. Include deleted records if set to queryAll.
  try {
    if (!$options['queryAll']) {
      $result = $sf->client
    else {
      $query = preg_replace("@SELECT\\s+@si", "SELECT IsDeleted, ", $query);
      $result = $sf->client
  } catch (Exception $e) {
    salesforce_api_log(SALESFORCE_LOG_ALL, 'Salesforce query failed with exception: ' . $e
      ->getMessage(), array(), WATCHDOG_ERROR);
    return FALSE;
  $records = $result->records;

  // If set to queryMore and this query hasn't retrieved all the results, then query for the rest.
  // @todo: Find a resultset large enough to test this.
  if (!$result->done && $options['queryMore']) {
    $moreRecords = _salesforce_api_querymore($result->queryLocator, $sf);
    if (is_array($moreRecords)) {
      $records = array_merge($records, $moreRecords);

  // Return records if any have been retrieved, or else return FALSE.
  return is_array($records) ? $records : FALSE;