You are here

function salesforce_pull_get_pull_query in Salesforce Suite 7.3

Given a SObject type name, build an SOQL query to include all fields for all SalesforceMappings mapped to that SObject.

Parameters

string $type: e.g. "Contact", "Account", etc.

Return value

SalesforceSelectQuery or NULL if no mappings or no mapped fields were found.

See also

SalesforceMapping::getMappedFields

SalesforceMapping::getMappedRecordTypes

1 call to salesforce_pull_get_pull_query()
salesforce_pull_get_updated_records in modules/salesforce_pull/salesforce_pull.module
Pull updated records from Salesforce and place them in the queue.

File

modules/salesforce_pull/salesforce_pull.module, line 296
Pull updates from Salesforce when a Salesforce object is updated.

Code

function salesforce_pull_get_pull_query($type) {
  $mapped_fields = array();
  $mapped_record_types = array();

  // Iterate over each field mapping to determine our query parameters.
  foreach (salesforce_mapping_load_multiple(array(
    'salesforce_object_type' => $type,
  )) as $mapping) {

    // Check the sync settings for create or update trigger.
    if (!($mapping->sync_triggers & (SALESFORCE_MAPPING_SYNC_SF_CREATE | SALESFORCE_MAPPING_SYNC_SF_UPDATE))) {

      // Skip this mapping.
      continue;
    }
    $mapped_fields = array_merge($mapped_fields, $mapping
      ->getMappedFields(array(
      SALESFORCE_MAPPING_DIRECTION_SYNC,
      SALESFORCE_MAPPING_DIRECTION_SF_DRUPAL,
    )));

    // If Record Type is specified, restrict query.
    $mapping_record_types = $mapping
      ->getMappedRecordTypes();

    // If Record Type is not specified for a given mapping, ensure query is unrestricted.
    if (empty($mapping_record_types)) {
      $mapped_record_types = FALSE;
    }
    elseif (is_array($mapped_record_types)) {
      $mapped_record_types = array_merge($mapped_record_types, $mapping_record_types);
    }
  }

  // There are no field mappings configured to pull data from Salesforce so
  // move on to the next mapped object. Prevents querying unmapped data.
  if (empty($mapped_fields)) {
    return NULL;
  }
  $soql = new SalesforceSelectQuery($type);

  // Convert field mappings to SOQL.
  $soql->fields = array_merge($mapped_fields, array(
    'Id' => 'Id',
    $mapping->pull_trigger_date => $mapping->pull_trigger_date,
  ));

  // If no lastupdate, get all records, else get records since last pull.
  $sf_last_sync = variable_get('salesforce_pull_last_sync_' . $type, NULL);
  if ($sf_last_sync) {
    $last_sync = gmdate('Y-m-d\\TH:i:s\\Z', $sf_last_sync);
    $soql
      ->addCondition($mapping->pull_trigger_date, $last_sync, '>');
  }
  if (!empty($mapped_record_types)) {
    $soql
      ->addCondition('RecordTypeId', $mapped_record_types, 'IN');
  }
  return $soql;
}