You are here

function sf_prematch_export in Salesforce Suite 7

Same name and namespace in other branches
  1. 6.2 sf_prematch/sf_prematch.main.inc \sf_prematch_export()
  2. 7.2 sf_prematch/sf_prematch.main.inc \sf_prematch_export()

Use prematch rule to find a salesforce object to match the node.

Parameters

stdObject $drupal_object - the actual instance of the entity:

array $map:

array $match_by:

Return value

string sfid

1 call to sf_prematch_export()
sf_prematch_sf_find_match in sf_prematch/sf_prematch.module
Implement hook_sf_find_match

File

sf_prematch/sf_prematch.main.inc, line 16
Import/Export functions for sf_prematch module.

Code

function sf_prematch_export($drupal_object, $map, $match_by) {
  if (empty($map['drupal']) || empty($match_by)) {
    return;
  }

  // Get information to allow using handlers to get values of Drupal fields.
  $drupal_object_info = salesforce_api_fieldmap_objects_load('drupal', $map['drupal_entity'], $map['drupal_bundle']);
  $drupal_fields_info = $drupal_object_info['fields'];
  $sf_object_info = salesforce_api_fieldmap_objects_load('salesforce', $map['salesforce'], $map['salesforce']);
  $values = array();
  $select_clause = "SELECT id";
  $drupal_to_salesforce_fieldmap = array_flip($map['fields']);

  // Create $values array allowing easy mapping from match_by fields to query fields and values.
  // Also build out SOQL select clause to include all match by sf fields.
  foreach ($match_by as $match_by_field => $drupal_field_name) {
    if ($match_by_field == 'fieldmap' || $match_by_field == 'rule') {
      continue;
    }
    $values[$match_by_field] = array();

    // If a handler is specified for retrieving a value for the source field...
    if (isset($drupal_fields_info[$drupal_field_name]['export'])) {
      $drupal_field_definition = $drupal_fields_info[$drupal_field_name];
      $sf_field_definition = $sf_object_info['fields'][$drupal_to_salesforce_fieldmap[$drupal_field_name]];

      // Get the value for the field from the handler function.
      $function = $drupal_fields_info[$drupal_field_name]['export'];
      $drupal_value = $function($drupal_object, $drupal_field_name, $drupal_field_definition, $sf_field_definition);
    }
    elseif (isset($drupal_object->{$drupal_field_name})) {
      $drupal_value = $drupal_object->{$drupal_field_name};
    }
    else {
      $drupal_value = null;
    }
    $values[$match_by_field]['drupal_value'] = $drupal_value;
    $values[$match_by_field]['salesforce_field_name'] = $drupal_to_salesforce_fieldmap[$drupal_field_name];

    // If there's a salesforce field to match by, include it in the select clause.
    if ($values[$match_by_field]['salesforce_field_name']) {
      $select_clause .= ', ' . $values[$match_by_field]['salesforce_field_name'];
    }
  }
  $sf_class = $map['salesforce'];
  $from_where_clause = " FROM {$sf_class} WHERE ";

  // Use match by rule to build out where clause.
  switch ($match_by['rule']) {
    case SF_PREMATCH_PRIMARY_SECONDARY_AND_TERTIARY:
      $from_where_clause .= $values['tertiary_field']['salesforce_field_name'] . " = '" . $values['tertiary_field']['drupal_value'] . "' AND ";

    // no break;
    case SF_PREMATCH_PRIMARY_AND_SECONDARY:
      $from_where_clause .= $values['secondary_field']['salesforce_field_name'] . " = '" . $values['secondary_field']['drupal_value'] . "' AND ";

    // no break;
    case SF_PREMATCH_PRIMARY:
      $from_where_clause .= $values['primary_field']['salesforce_field_name'] . " = '" . $values['primary_field']['drupal_value'] . "'";
      break;
    default:
      return;
      break;
  }
  $query = $select_clause . $from_where_clause;

  // Run the SOQL query against the Salesforce API
  if ($sf = salesforce_api_connect()) {
    try {
      $result = $sf->client
        ->query($query);
    } catch (Exception $e) {
      DrupalSalesforce::watchdog(SALESFORCE_LOG_SOME, 'Exception in sf_prematch stage: ' . $e
        ->getMessage(), array(), WATCHDOG_ALERT);
      return;
    }
    switch ($result->size) {
      case 0:
        return;
        break;
      case 1:
        return $result->records[0]->Id;
        break;
      default:

        // TODO: handle multiple records
        break;
    }
  }
}