You are here

public function SalesforcePullCommands::pullQuery in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/salesforce_pull/src/Commands/SalesforcePullCommands.php \Drupal\salesforce_pull\Commands\SalesforcePullCommands::pullQuery()
  2. 8.3 modules/salesforce_pull/src/Commands/SalesforcePullCommands.php \Drupal\salesforce_pull\Commands\SalesforcePullCommands::pullQuery()

Given a mapping, enqueue records for pull from Salesforce.

Ignoring modification timestamp. This command is useful, for example, when seeding content for a Drupal site prior to deployment.

@option where A WHERE clause to add to the SOQL pull query. Default behavior is to query and pull all records. @option start strtotime()able string for the start timeframe over which to pull, e.g. "-5 hours". If omitted, use the value given by the mapping's pull timestamp. Must be in the past. @option stop strtotime()able string for the end timeframe over which to pull, e.g. "-5 hours". If omitted, defaults to "now". Must be "now" or earlier. @option force-pull if given, force all queried records to be pulled regardless of updated timestamps. If omitted, only Salesforce records which are newer than linked Drupal records will be pulled. @usage drush sfpq user Query and queue all records for "user" Salesforce mapping. @usage drush sfpq user --where="Email like '%foo%' AND (LastName = 'bar' OR FirstName = 'bar')" Query and queue all records for "user" Salesforce mapping with Email field containing the string "foo" and First or Last name equal to "bar" @usage drush sfpq Fetch and process all pull queue items @usage drush sfpq --start="-25 minutes" --stop="-5 minutes" Fetch updated records for all mappings between 25 minutes and 5 minutes old, and process them. @usage drush sfpq foo --start="-25 minutes" --stop="-5 minutes" Fetch updated records for mapping "foo" between 25 minutes and 5 minutes old, and process them.

@command salesforce_pull:pull-query @aliases sfpq,sfiq,sf-pull-query,salesforce_pull:query

Parameters

string $name: Machine name of the Salesforce Mapping for which to queue pull records.

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

Throws

\Exception

File

modules/salesforce_pull/src/Commands/SalesforcePullCommands.php, line 161

Class

SalesforcePullCommands
A Drush commandfile.

Namespace

Drupal\salesforce_pull\Commands

Code

public function pullQuery($name, array $options = [
  'where' => '',
  'start' => 0,
  'stop' => 0,
  'force-pull' => FALSE,
]) {
  $mappings = $this
    ->getPullMappingsFromName($name);
  $start = $options['start'] ? strtotime($options['start']) : 0;
  $stop = $options['stop'] ? strtotime($options['stop']) : 0;
  if ($start > $stop) {
    $this
      ->logger()
      ->error(dt('Stop date-time must be later than start date-time.'));
    return;
  }
  foreach ($mappings as $mapping) {
    if (!($soql = $mapping
      ->getPullQuery([], $start, $stop))) {
      $this
        ->logger()
        ->error(dt('!mapping: Unable to generate pull query. Does this mapping have any Salesforce Action Triggers enabled?', [
        '!mapping' => $mapping
          ->id(),
      ]));
      continue;
    }
    if ($options['where']) {
      $soql->conditions[] = [
        $options['where'],
      ];
    }
    $this->eventDispatcher
      ->dispatch(new SalesforceQueryEvent($mapping, $soql), SalesforceEvents::PULL_QUERY);
    $this
      ->logger()
      ->info(dt('!mapping: Issuing pull query: !query', [
      '!query' => (string) $soql,
      '!mapping' => $mapping
        ->id(),
    ]));
    $results = $this->client
      ->query($soql);
    if (empty($results)) {
      $this
        ->logger()
        ->warning(dt('!mapping: No records found to pull.', [
        '!mapping' => $mapping
          ->id(),
      ]));
      return;
    }
    $this->pullQueue
      ->enqueueAllResults($mapping, $results, $options['force-pull']);
    $this
      ->logger()
      ->info(dt('!mapping: Queued !count items for pull.', [
      '!count' => $results
        ->size(),
      '!mapping' => $mapping
        ->id(),
    ]));
  }
}