You are here

function drush_salesforce_sf_query_object in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 salesforce.drush.inc \drush_salesforce_sf_query_object()
  2. 7.3 salesforce.drush.inc \drush_salesforce_sf_query_object()
  3. 5.0.x salesforce.drush.inc \drush_salesforce_sf_query_object()

Query Salesforce objects available to the logged-in user.

Parameters

string $name: The object type name, e.g. Account.

File

./salesforce.drush.inc, line 377
Drush integration for Salesforce.

Code

function drush_salesforce_sf_query_object($name) {
  _drush_salesforce_deprecated();
  $salesforce = \Drupal::service('salesforce.client');
  $query = new SelectQuery($name);
  $fields = drush_get_option('fields', '');
  if (!$fields) {
    $object = $salesforce
      ->objectDescribe($name);
    $query->fields = array_keys($object
      ->getFields());
  }
  else {
    $query->fields = explode(',', $fields);
  }
  $query->limit = drush_get_option('limit', '');
  if ($where = drush_get_option('where', '')) {
    $query->conditions = [
      [
        $where,
      ],
    ];
  }
  if ($order = drush_get_option('order', '')) {
    $query->order = [];
    $orders = explode(',', $order);
    foreach ($orders as $order) {
      list($field, $dir) = preg_split('/\\s+/', $order, 2);
      $query->order[$field] = $dir;
    }
  }
  try {
    $result = $salesforce
      ->query($query);
  } catch (SalesforceException $e) {
    drush_log($e
      ->getMessage(), 'error');
    return;
  }
  foreach ($result
    ->records() as $sfid => $record) {
    drush_print(drush_format([
      $sfid => $record
        ->fields(),
    ]));
  }
  $pretty_query = str_replace('+', ' ', (string) $query);
  if (!$fields) {
    $fields = implode(',', $query->fields);
    $pretty_query = str_replace($fields, ' * ', $pretty_query);
  }
  drush_print(dt("Showing !size of !total records for query:\n!query", [
    '!size' => count($result
      ->records()),
    '!total' => $result
      ->size(),
    '!query' => $pretty_query,
  ]));
}