You are here

function sf_user_import in Salesforce Suite 5.2

Same name and namespace in other branches
  1. 6.2 sf_user/sf_user.module \sf_user_import()

Imports data from Salesforce into a user.

Parameters

$sfid: The Salesforce ID of the object from which you want to import.

$fieldmap: The index of the fieldmap to use to create the export object.

$uid: The uid of the user to update. If left NULL, a new user will be created.

Return value

The uid of the imported user or FALSE on failure.

1 call to sf_user_import()
sf_user_salesforce_form_submit in sf_user/sf_user.module

File

sf_user/sf_user.module, line 377
Integrates the core user object and profile module with the Salesforce API.

Code

function sf_user_import($sfid, $fieldmap, $uid = NULL) {

  // Retrieve the object from Salesforce.
  $sf = salesforce_api_connect();
  $data = $sf
    ->retrieve(array(
    $sfid,
  ), $fieldmap);

  // Return FALSE if the object data was not found at Salesforce.
  if (empty($data)) {
    return FALSE;
  }

  // Load the fieldmap data.
  $map = salesforce_api_fieldmap_load($fieldmap);

  // Load the object definitions.
  $drupal_object = salesforce_api_fieldmap_objects_load('drupal', $map['drupal']);
  $salesforce_object = salesforce_api_fieldmap_objects_load('salesforce', $map['salesforce']);

  // If a node was specified, attempt to load it.
  $account = user_load(array(
    'uid' => $uid,
  ));

  // If the node exists, simply update the existing node.
  if ($account->uid) {

    // Loop through the fields on the fieldmap.
    foreach ($map['fields'] as $value => $key) {

      // If a handler is specified for importing a value from Salesforce.
      if (isset($drupal_object['fields'][$key]['import'])) {

        // Get the value for the field from the handler function.
        $change = $drupal_object['fields'][$key]['import']($account, $key, $data, $value);
        $changes = array_merge($changes, $change);
      }
      elseif (isset($data->{$value})) {

        // Otherwise set the field on the export object to the value of the source
        // field if it's present on the source object.
        if ($account->{$key} != $data->{$value}) {
          $changes[$key] = $data->{$value};
        }
      }
    }

    //drupal_set_message('<pre>'. print_r($changes, TRUE) .'</pre>');
    user_save($account, $changes);
  }
  return $account->uid;
}