You are here

public function SalesforceCommands::queryObject in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 src/Commands/SalesforceCommands.php \Drupal\salesforce\Commands\SalesforceCommands::queryObject()
  2. 5.0.x src/Commands/SalesforceCommands.php \Drupal\salesforce\Commands\SalesforceCommands::queryObject()

Query an object using SOQL with specified conditions.

@option where A WHERE clause to add to the SOQL query @option fields A comma-separated list fields to select in the SOQL query. If absent, an API call is used to find all fields @option limit Integer limit on the number of results to return for the query. @option order Comma-separated fields by which to sort results. Make sure to enclose in quotes for any whitespace.

@command salesforce:query-object @aliases sfqo,sf-query-object

Parameters

string $object: The object type name in Salesforce (e.g. Account).

array $options: An associative array of options whose values come from cli, aliases, config, etc.

Return value

\Drupal\salesforce\Commands\QueryResult The query result.

Throws

\Exception

File

src/Commands/SalesforceCommands.php, line 619

Class

SalesforceCommands
A Drush commandfile.

Namespace

Drupal\salesforce\Commands

Code

public function queryObject($object, array $options = [
  'format' => 'table',
  'where' => NULL,
  'fields' => NULL,
  'limit' => NULL,
  'order' => NULL,
]) {
  $query = new SelectQuery($object);
  if (!$options['fields']) {
    $object = $this->client
      ->objectDescribe($object);
    $query->fields = array_keys($object
      ->getFields());
  }
  else {
    $query->fields = explode(',', $options['fields']);

    // Query must include Id.
    if (!in_array('Id', $query->fields)) {
      $query->fields[] = 'Id';
    }
  }
  $query->limit = $options['limit'];
  if ($options['where']) {
    $query->conditions = [
      [
        $options['where'],
      ],
    ];
  }
  if ($options['order']) {
    $query->order = [];
    $orders = explode(',', $options['order']);
    foreach ($orders as $order) {
      list($field, $dir) = preg_split('/\\s+/', $order, 2);
      $query->order[$field] = $dir;
    }
  }
  return $this
    ->returnQueryResult(new QueryResult($query, $this->client
    ->query($query)));
}