function sf_user_export in Salesforce Suite 6.2
Same name and namespace in other branches
- 5.2 sf_user/sf_user.module \sf_user_export()
Exports a user to Salesforce using the specified fieldmap and stores the ID of the Salesforce object for the user.
Parameters
$uid: The uid of the user to export.
$name: The name of the fieldmap to use to create the export object.
$sfid: The Salesforce ID of the object you want to update. If left NULL, a new object will be created at Salesforce.
Return value
TRUE or FALSE indicating the success of the operation.
2 calls to sf_user_export()
- sf_user_salesforce_form_submit in sf_user/
sf_user.module - sf_user_user in sf_user/
sf_user.module - Implementation of hook_user().
File
- sf_user/
sf_user.module, line 473 - Integrates the core user object and profile module with the Salesforce API.
Code
function sf_user_export($uid, $name, $sfid = NULL) {
// Attempt to connect to Salesforce.
$sf = salesforce_api_connect();
if (!$sf) {
// Let modules react to a failure to export this user.
module_invoke_all('salesforce_api_export_connect_fail', $uid, $name, $sfid);
if (user_access('administer salesforce')) {
drupal_set_message(t('Unable to connect to Salesforce using <a href="!url">current credentials</a>.', array(
'!url' => url(SALESFORCE_PATH_ADMIN),
)));
}
return FALSE;
}
// Load the user.
$account = user_load(array(
'uid' => $uid,
));
// Create an object for export based on the specified fieldmap.
$object = salesforce_api_fieldmap_export_create($name, $account);
// Load the fieldmap so we can get the object name.
$map = salesforce_api_fieldmap_load($name);
if (empty($sfid)) {
// Look for any matching records which we might want to update instead of creating duplicates.
$matches = salesforce_api_search_for_duplicates('export', 'user', $account, $name);
if (!empty($matches)) {
$object->Id = $sfid = reset($matches);
$account->salesforce = array(
'sfid' => $sfid,
'name' => $name,
);
salesforce_api_id_save('user', $account->uid, $sfid, $name, 'export');
}
}
else {
$object->Id = $sfid;
}
$fieldmap_name = $name;
foreach (module_implements('salesforce_api_pre_export') as $module) {
$function = $module . '_salesforce_api_pre_export';
$continue = $function($object, $map, $account->uid);
if ($continue === FALSE) {
return;
}
}
// If any modules altered the fieldmap, ensure that the new fieldmap name
// is used for the rest of the export.
if ($map->name != $name) {
$name = $map->name;
}
try {
$response = $sf->client
->upsert('Id', array(
$object,
), $map->salesforce);
} catch (Exception $e) {
salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception while attempting to export user: %msg <pre>%e</pre>', array(
'%msg' => $e
->getMessage(),
'%e' => print_r($e, TRUE),
), WATCHDOG_ERROR, l('user ' . $account->uid, 'user/' . $account->uid));
}
// Check to see if response is an array of objects
if (is_array($response) && count($response) > 0) {
$response = $response[0];
}
// If we got errors, handle them before proceeding
if (is_object($response->errors)) {
// If we got "Entity is deleted" and we're configured to unlink and upsert,
// do it. @see sf_node_export()
if ($response->errors->statusCode == 'ENTITY_IS_DELETED' && ($response->errors->fields == 'Id' || empty($response->errors->fields)) && SALESFORCE_DELETED_POLICY_UPSERT == variable_get('salesforce_api_entity_deleted_policy', SALESFORCE_DELETED_POLICY_UPSERT)) {
salesforce_api_id_unlink(array(
'sfid' => $object->Id,
));
$account->salesforce->sfid = $object->Id = NULL;
// Look for any matching records which we might want to update instead of creating duplicates.
$matches = salesforce_api_search_for_duplicates('export', 'user', $account, $name);
if (!empty($matches)) {
$object->Id = reset($matches);
}
salesforce_api_log(SALESFORCE_LOG_SOME, 'Salesforce entity deleted. Attempting to unlink and upsert. <pre>%response</pre>', array(
'%response' => print_r($response, 1),
), WATCHDOG_ERROR, l('user ' . $uid, 'user/' . $uid));
try {
$response = $sf->client
->upsert('Id', array(
$object,
), $map->salesforce);
} catch (Exception $e) {
salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception while attempting to export user: %msg <pre>%e</pre>', array(
'%msg' => $e
->getMessage(),
'%e' => print_r($e, TRUE),
), WATCHDOG_ERROR, l('user ' . $account->uid, 'user/' . $account->uid));
}
}
}
// If the export was successful...
if ($response->success) {
// Store the Salesforce ID for the user and return TRUE.
salesforce_api_id_save('user', $uid, $response->id, $name, 'export');
}
else {
// Otherwise log the error and return FALSE.
if (user_access('administer salesforce')) {
if (function_exists('dsm')) {
dsm($response);
}
else {
if (user_access('administer salesforce')) {
drupal_set_message(t('Salesforce Suite export error: <pre>%response</pre>', array(
'%response' => print_r($response, 1),
)), 'error');
}
}
}
salesforce_api_log(SALESFORCE_LOG_SOME, 'Salesforce returned an unsuccessful response: <pre>%response</pre>', array(
'%response' => print_r($response, 1),
), WATCHDOG_ERROR, l('user ' . $account->uid, 'user/' . $account->uid));
}
module_invoke_all('salesforce_api_post_export', $object, $name, $uid, $response);
return $response->success;
}