You are here

function sf_import_import_records in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 7.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 130

Code

function sf_import_import_records() {
  $fieldmaps = variable_get('sf_import_fieldmaps', salesforce_api_salesforce_field_map_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 time()
  foreach ($active_fieldmaps as $map) {
    $map = salesforce_api_salesforce_field_map_load($map);
    $sql = "SELECT time FROM {sf_import_queue} ORDER BY time DESC LIMIT 1";
    $start = db_result(db_query($sql));
    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', time() - 3600);
    }
    $end = 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 enusre 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', 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;

      // Handle single record
      if (!is_array($update_sfids)) {
        $update_sfids = array(
          $update_sfids,
        );
      }
      foreach ($update_sfids as $sfid) {
        $sql = "SELECT sfid FROM {sf_import_queue} WHERE sfid = '%s'";
        $exists = db_result(db_query($sql, $sfid));
        if (!$exists) {
          $object->time = time();
          $object->sfid = $sfid;
          $object->fieldmap = $map->name;
          $ret = drupal_write_record('sf_import_queue', $object);
        }
        $records[] = array(
          $sfid,
          $map->name,
          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;
  }
}