You are here

function salesforce_pull_get_deleted_records in Salesforce Suite 7.3

Get deleted records from salesforce.

1 call to salesforce_pull_get_deleted_records()
salesforce_pull in modules/salesforce_pull/salesforce_pull.module
Callback for the standard pull process used by webhooks and cron.

File

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

Code

function salesforce_pull_get_deleted_records() {

  // Calculate which client to use. We default to the REST client but also
  // support SOAP if enabled. Note that deletions can only be queried via REST
  // with an API version >= 29.0.
  $sfapi = salesforce_get_api();
  $client = $sfapi;
  $use_soap = FALSE;
  if (class_exists('SalesforceSoapPartner')) {
    $client = new SalesforceSoapPartner($sfapi, variable_get('salesforce_partner_wsdl', libraries_get_path('salesforce') . '/soapclient/partner.wsdl.xml'));
    $use_soap = TRUE;
  }

  // Before we delete any records, we must check for merged records.
  $merge_record_type = variable_get(SALESFORCE_MERGE_RECORD, FALSE);
  if (!empty($merge_record_type)) {
    $merged_results = salesforce_pull_check_merged_records($merge_record_type);
  }

  // Load all unique SF record types that we have mappings for.
  foreach (array_reverse(salesforce_mapping_get_mapped_objects()) as $type) {
    $last_delete_sync = variable_get('salesforce_pull_delete_last_' . $type, REQUEST_TIME);
    variable_set('salesforce_pull_delete_last_' . $type, REQUEST_TIME);
    $version_path = parse_url($sfapi
      ->getApiEndPoint(), PHP_URL_PATH);
    $queue = DrupalQueue::get(SALESFORCE_PULL_QUEUE);
    $now = time();

    // getDeleted() constraint: startDate cannot be more than 30 days ago
    // (using an incompatible data may lead to exceptions).
    $last_delete_sync = $last_delete_sync > REQUEST_TIME - 2505600 ? $last_delete_sync : REQUEST_TIME - 2505600;

    // getDeleted() constraint: startDate must be at least one minute greater
    // than endDate.
    $now = $now > $last_delete_sync + 60 ? $now : $now + 60;
    $last_delete_sync_sf = gmdate('Y-m-d\\TH:i:s\\Z', $last_delete_sync);
    $now_sf = gmdate('Y-m-d\\TH:i:s\\Z', $now);

    // SOAP getDeleted() returns an object while the REST getDeleted() returns
    // an array, so cast all checked values to an object to avoid additional
    // conditional logic.
    if ($use_soap) {
      $results = (object) $client
        ->trySoap('getDeleted', $type, $last_delete_sync_sf, $now_sf);
    }
    else {
      $results = (object) $client
        ->getDeleted($type, $last_delete_sync_sf, $now_sf);
    }
    if (empty($results->deletedRecords)) {
      continue;
    }
    if (!isset($results->errorCode)) {

      // Convert deleted records to objects.
      foreach ($results->deletedRecords as $index => $result) {
        $results->deletedRecords[$index] = (object) $result;
      }

      // Handle merges from Salesforce.
      $merge_fields = array(
        'old_contact_key' => 'Old_Contact__c',
        'merged_contact_key' => 'Contact__c',
      );
      if (!empty($merge_record_type)) {
        drupal_alter('salesforce_pull_entity_merge_fields', $merge_fields);
        foreach ($results->deletedRecords as $result) {
          foreach ($merged_results['records'] as $merged_result) {
            if ($result->id == $merged_result[$merge_fields['old_contact_key']]) {
              $queue_item = array(
                'merge' => $merged_result,
              );
              $queue
                ->createItem($queue_item);
            }
          }
        }
      }

      // Write items to the queue.
      foreach ($results->deletedRecords as $result) {

        // Cast to array for consistency.
        $result = (array) $result;
        $result['type'] = $type;
        $queue_item = array(
          'delete' => $result,
        );
        $queue
          ->createItem($queue_item);
      }

      // Handle requests larger than the batch limit (usually 2000).
      $next_records_url = isset($results->nextRecordsUrl) ? str_replace($version_path, '', $results->nextRecordsUrl) : FALSE;
      while ($next_records_url) {
        $new_result = $sfapi
          ->apiCall($next_records_url);
        if (!isset($new_result['errorCode'])) {
          if (!empty($merge_record_type)) {
            foreach ($results->deletedRecords as $result) {
              foreach ($merged_results['records'] as $merged_result) {
                if ($result->id == $merged_result[$merge_fields['old_contact_key']]) {
                  $queue_item = array(
                    'merge' => $merged_result,
                  );
                  $queue
                    ->createItem($queue_item);
                }
              }
            }
          }

          // Write items to the queue.
          foreach ($new_result['records'] as $result) {
            $queue_item = array(
              'delete' => $result,
            );
            $queue
              ->createItem($queue_item);
          }
        }
        $next_records_url = isset($new_result->nextRecordsUrl) ? str_replace($version_path, '', $new_result->nextRecordsUrl) : FALSE;
      }
      variable_set('salesforce_pull_last_sync_' . $type, REQUEST_TIME);
    }
  }
}