You are here

function _sf_notifications_handle_message in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 7.2 sf_notifications/sf_notifications.module \_sf_notifications_handle_message()

Loop through an array of SObjects from SalesForce and save them according to any existing sf fieldmaps, notification settings, and data.

Parameters

array $objects: A numerically indexed array of SObjects (as returned by _sf_notifications_parse_message())

Return value

(boolean) FALSE if there were errors. TRUE otherwise.

See also

sf_notifications_fieldmap_settings()

sf_notifications_settings_form()

1 call to _sf_notifications_handle_message()
sf_notifications_endpoint in sf_notifications/sf_notifications.module
Menu callback for SalesForce notifications endpoint @todo Add authentication. see "Downloading the Salesforce.com Client Certificate" at http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax...

File

sf_notifications/sf_notifications.module, line 164

Code

function _sf_notifications_handle_message($objects) {
  $success = TRUE;

  // For each object received from Salesforce, gather all relevant fieldmaps.
  // For each relevant fieldmap, perform the appropriate C(r)UD operation.
  $new_records = $objects['salesforce'];
  $active = variable_get('sf_notifications_active_maps', array());
  $active = array_filter($active);
  foreach ($objects['drupal'] as $object_record) {
    $sfid = $object_record['sfid'];
    $obj = $objects['salesforce'][$sfid];

    // We'll handle inserts later on
    unset($new_records[$sfid]);

    // Break on fieldmap-specific conditions
    $map = salesforce_api_fieldmap_load($object_record['name']);
    if (empty($active[$map->name])) {
      continue;
    }
    $operation = $obj->fields->IsDeleted == 'true' ? 'delete' : 'update';
    $object_record['fields'] = $obj->fields;
    $object_record['operation'] = $operation;
    if (!sf_notifications_check_condition($operation, $object_record, $map)) {
      continue;
    }
    switch ($operation) {
      case 'delete':
        $success = $success && sf_notifications_delete_record($object_record);
        break;
      case 'update':
        $success = $success && sf_notifications_update_record($object_record);
        break;
    }
  }
  foreach ($new_records as $sfid => $obj) {
    $maps = salesforce_api_salesforce_field_map_load_by(array(
      'salesforce' => $obj->type,
    ));
    if (empty($maps)) {
      salesforce_api_log(SALESFORCE_LOG_SOME, 'SalesForce Notifications: No fieldmap found.
            <pre>' . print_r($obj, 1) . '</pre>');
      $success = FALSE;
      continue;
    }

    // For each map, check active, check conditions and insert.
    foreach ($maps as $map) {
      if (empty($active[$map->name])) {
        continue;
      }

      // Forge an object record to proceed.
      // Insert is the same as update, just without an oid.
      $object_record = array(
        'oid' => NULL,
        'name' => $map->name,
        'drupal_type' => $fieldmap_type,
        'fields' => $obj->fields,
        'operation' => 'insert',
      );
      if (!sf_notifications_check_condition('insert', $object_record, $map)) {
        continue;
      }
      $success = $success && sf_notifications_update_record($object_record);
    }
  }

  // Clear the page and block caches, for good measure.
  cache_clear_all();
  return $success;
}