You are here

function sf_import_import_records in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 6.2 sf_import/sf_import.module \sf_import_import_records()

This function is called on cron run. It is responsible for calling functions to import records using the getUpdated() method or a custom SOQL query, depending on what the user selected in admin settings for sf_import.

3 calls to sf_import_import_records()
drush_sf_import_sf_get_updated in sf_import/sf_import.drush.inc
Calls SF Import Get Updated
sf_import_cron in sf_import/sf_import.module
Implements hook_cron().
sf_import_settings_form_submit in sf_import/sf_import.admin.inc
Submit handler for the settings page.

File

sf_import/sf_import.module, line 122

Code

function sf_import_import_records() {
  $fieldmaps = variable_get('sf_import_fieldmaps', salesforce_api_salesforce_fieldmap_load_all());
  $active_fieldmaps = array();
  foreach ($fieldmaps as $map_key => $map_value) {
    if ($map_value !== 0) {
      $active_fieldmaps[$map_key] = $map_value;
    }
  }
  if (!$active_fieldmaps) {
    return FALSE;
  }
  $records = array();

  // Get updated and/or deleted items for each fieldmap and store in sf_import_queue.
  // Start date is newest date of SFID stored in sf_import_queue, end date is REQUEST_TIME.
  foreach ($active_fieldmaps as $name => $map) {
    $map = salesforce_api_salesforce_fieldmap_load($name);

    // Skip to next fieldmap if the fieldmap couldn't be loaded
    // (such as when it doesn't exist in the database).
    if (!is_object($map)) {
      continue;
    }
    $start = db_query_range("SELECT time FROM {sf_import_queue} ORDER BY time", 0, 1)
      ->fetchField();
    if (!$start) {

      // If $start isn't set, then set the start to an hour back from the current time
      $start = variable_get('sf_import_queue_last_import', REQUEST_TIME - 3600);
    }
    $end = REQUEST_TIME;
    $import_method = variable_get('sf_import_' . $map->name . '_update_method', 'get_updated');

    // If the last time we checked for updated records was within the last
    // hour, then push the $start value back an hour.
    // This helps ensure that we don't skip over any updated records.
    // @todo Make this an admin configurable option.
    if ($end - $start < 3600 && $import_method == 'get_updated') {
      $start = $start - 3600;
    }

    // Set the time that the last import took place.
    variable_set('sf_import_queue_last_import', REQUEST_TIME);
    $import_function = $import_method == 'get_updated' ? 'salesforce_api_get_updated' : '_sf_import_get_soql_records';
    if ($updates = $import_function($map, $start, $end)) {
      $update_sfids = $updates->ids;

      // If there is only a single updated record, convert it to an array.
      if (!is_array($update_sfids)) {
        $update_sfids = array(
          $update_sfids,
        );
      }
      foreach ($update_sfids as $sfid) {
        $exists = db_query("SELECT sfid FROM {sf_import_queue} WHERE sfid = :sfid", array(
          ':sfid' => $sfid,
        ))
          ->fetchField();
        if (!$exists) {
          $object->time = REQUEST_TIME;
          $object->sfid = $sfid;
          $object->fieldmap = $map->name;

          // @todo: Replace the deprecated drupal_write_record() with db_update().
          $ret = drupal_write_record('sf_import_queue', $object);
        }
        $records[] = array(
          $sfid,
          $map->name,
          REQUEST_TIME,
        );
      }
    }
  }
  if (count($records) > 0) {
    variable_set('sf_import_queue_import_count', count($records));
    return $records;
  }
  else {
    variable_set('sf_import_queue_import_count', 0);
    return FALSE;
  }
}