function sf_entity_export in Salesforce Suite 7.2
Same name and namespace in other branches
- 7 sf_entity/sf_entity.module \sf_entity_export()
Exports an entity to Salesforce using the specified fieldmap and stores the ID of the Salesforce object for the entity.
Parameters
object $entity: The entity to export
string $name: The name of the fieldmap used to create the export object.
string $sfid: The Salesforce ID of the object you want to update. If left NULL, a new object will be created at Salesforce.
Return value
bool TRUE or FALSE indicating the success of the operation.
2 calls to sf_entity_export()
- sf_entity_salesforce_form_submit in sf_entity/
sf_entity.module - Form submit handler for the [entity-name]/%/salesforce form.
- sf_entity_save in sf_entity/
sf_entity.module
File
- sf_entity/
sf_entity.module, line 781 - Integrates fieldable entities with the Salesforce API.
Code
function sf_entity_export($entity, $name, $sfid = NULL) {
// Load the fieldmap so we can get the object name.
$map = salesforce_api_salesforce_fieldmap_load($name);
list($id, $vid, $bundle) = entity_extract_ids($map->drupal_entity, $entity);
// Attempt to connect to Salesforce.
$sf = salesforce_api_connect();
if (!isset($sf) || !is_object($sf)) {
// Let modules react to a failure to export this entity.
module_invoke_all('salesforce_api_export_connect_fail', $id, $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;
}
if (empty($entity)) {
salesforce_api_log(SALESFORCE_LOG_SOME, 'sf_entity_export was provided an empty entity to export: ' . print_r($entity, 1), array(), WATCHDOG_ERROR);
return FALSE;
}
// Look for any matching records which we might want to update instead of creating duplicates.
if (empty($sfid)) {
$matches = salesforce_api_search_for_duplicates('export', $map->drupal_entity, $map->drupal_bundle, $entity, $name);
if (!empty($matches)) {
$sfid = reset($matches);
$entity->salesforce->sfid = $sfid;
salesforce_api_id_save($id, $sfid, $name, $map->drupal_entity, $map->drupal_bundle, 'export');
$e = entity_load($map->drupal_entity, array(
$id,
), array(), TRUE);
$entity = $e[$id];
}
}
// Create an object for export based on the specified fieldmap.
$object = salesforce_api_fieldmap_export_create($name, $entity);
$object->Id = $sfid;
// Allow modules to alter the Salesforce object and fieldmap prior to export.
foreach (module_implements('salesforce_api_pre_export') as $module) {
$function = $module . '_salesforce_api_pre_export';
$continue = $function($object, $map, $id);
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) {
$uri = entity_uri($map->drupal_entity, $entity);
$link = NULL;
if (isset($uri['path'])) {
$link = l('view', $uri['path']);
}
salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception while attempting to upsert entity: %msg <pre>%e</pre>', array(
'%msg' => $e
->getMessage(),
'%e' => print_r($e, TRUE),
), WATCHDOG_ERROR, $link);
}
// 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 (isset($response->errors) && is_object($response->errors)) {
// If we got "Entity is deleted" and we're configured to unlink and upsert,
// do it.
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)) {
// If the entity is deleted, unlink ALL the linked drupal objects.
salesforce_api_id_unlink(array(
'sfid' => $object->Id,
));
$entity->salesforce->sfid = $object->Id = NULL;
// Look for any matching records which we might want to update instead of
// creating duplicates. Assume that salesforce_api_search_for_duplicates()
// will never return a deleted record.
$matches = salesforce_api_search_for_duplicates('export', $map->drupal_entity, $map->drupal_bundle, $entity, $name);
if (!empty($matches)) {
$sfid = reset($matches);
$entity->salesforce->sfid = $sfid;
}
$uri = entity_uri($map->drupal_entity, $entity);
$link = NULL;
if (isset($uri['path'])) {
$link = l('view', $uri['path']);
}
salesforce_api_log(SALESFORCE_LOG_SOME, 'Salesforce record deleted. Attempting to unlink and upsert. <pre>%response</pre>', array(
'%response' => print_r($response, 1),
), WATCHDOG_ERROR, $link);
try {
$response = $sf->client
->upsert('Id', array(
$object,
), $map->salesforce);
} catch (Exception $e) {
salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception while attempting to upsert entity: %msg <pre>%e</pre>', array(
'%msg' => $e
->getMessage(),
'%e' => print_r($e, TRUE),
), WATCHDOG_ERROR, l('view', $uri['path']));
}
}
}
// If the export was successful, save the Salesforce ID for the Drupal entity.
if (isset($response->success) && $response->success == TRUE) {
// Store the Salesforce ID for the entity and return TRUE.
salesforce_api_id_save($id, $response->id, $name, $map->drupal_entity, $map->drupal_bundle, 'export');
}
else {
// Otherwise log the error.
if (user_access('administer salesforce')) {
sf_dpm($response);
}
$uri = entity_uri($map->drupal_entity, $entity);
$link = NULL;
if (isset($uri['path'])) {
$link = l('entity ' . $id, $uri['path']);
}
salesforce_api_log(SALESFORCE_LOG_SOME, 'Salesforce returned an unsuccessful response: ' . print_r($response, 1), array(), WATCHDOG_ERROR, $link);
}
// Allow other modules to respond after an export.
module_invoke_all('salesforce_api_post_export', $object, $map, $id, $response);
// Return the status of the export.
$result = isset($response->success) && $response->success == TRUE ? TRUE : FALSE;
return $result;
}